Member Avatar for taimor

bf4dc2600477f6087a38bb2ec34ccd7f

what is the wrong?...this massage came when i decleration a pointer in my program and use it !!

Recommended Answers

All 7 Replies

line 23 maybe.

There would be a problem with your code. Please post your code.

Member Avatar for taimor
   void binary(int *p)
      { int z,c,t,b;
       int a=0;
       int *o=new int;
       if(o==NULL)
       cout<<"NO REVERSATION";
        else
       {
       for(int i=0;i<m;i++)
        a=a+*(p+i);
         a=a/m;
        while(a!=0)
        {
        z=a%2;
        *o=z;
        o++;
        c++;
        a=a/2;
        }
        o=o-c;
        b=c;
        for(int j=0;j<c/2; j++)
         { t=*(o+j);
           *(o+j)=*(o+b);
           *(o+b)=t;
            b--;
          }
           for(int u=0;u<c;u++)
           cout<<*(o+u);
           }
        }

((this code is a part of a big program which evaluate "The binary equivalent of the average"..
my program has a pointer which I declarate it as"int *p=new int[N];"
and when I declarate another pointer and use it ..i have this problem..!))

On line 4 you declare o as int *o=new int;. This ony gives you a single int pointer. So when you call line 16 you invalidate the pointer because you step out of the memory allocated to it. It looks to me like you need o to be an array. if you know what the size of o should be than make line 4 int *o=new int[size];, where size is the size of the array you need o to be. You are also incrementing c on line 17 but you never set c before you do that. You always want to set the value of a variable before you start using it.

Member Avatar for taimor

Thaaaaaaaaanks A lot bro.
you are awesome..
SO i need to use a code to help me to know the size of the array at first ..then i must declarate it as int *o=new int[size]; ..before using it ?

Yes. You need to figure out the size that you need o to be and then you use that size to allocate the memory you need with new.

Member Avatar for taimor

I did what you said..but there is no difference !!
this is my new code..
still have the same problem..:(
what is the wrong ?

                                                    void binary(int *p)
                                                    { int z,c=0,b;
                                                      int a=0;
                                                      for(int i=0;i<m;i++)
                                                      a=a+*(p+i);
                                                      a=a/m;
                                                      b=a;
                                                        while(a!=0)
                                                        {
                                                        a=a/2;
                                                        c++;
                                                        }
                                                        int *o=new int[c];
                                                        while(b!=0)
                                                        {
                                                        z=b%2;
                                                        b=b/2;
                                                        *o=z;
                                                        o++;
                                                        }
                                                        o=o-c;
                                                         for(int u=c;u>0;u--)
                                                         cout<<*(o+u);

                                                    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.