[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Pointers and clsases.

Hi all,

Headers.h
///////////////////////////
class MyPoint{
      private:
            float x;
            float y;
      public:
            void setX(float a){x=a;}
            float getX(){return x;}
            void setY(float a){y=a;}
            float getY(){return y;}
};

class MyClass{
      private:
            MyPoint *pcpointer;
      public:
            void myMethod();
};



Sources.cpp
///////////////////////////

void MyClass::myMethod(){
      pcpointer=new MyPoint[6];

      for(int i=0;i<6;i++){
            pcpointer[i].setX(555);
            pcpointer[i].setY(777);
      }

      delete pcpointer;
}


It fails on running, complaining of a "DAMAGE: after Normal block" error... I've checked this and it is something to do with overwriting an arrays bounds... Although commenting out the delete works fine then.

Thanks
0
Unimatrix_001
Asked:
Unimatrix_001
  • 6
  • 5
1 Solution
 
jkrCommented:
Well, here's one problem:

     delete pcpointer;

that should be

     delete [] pcpointer;

since you are allocating an array.
0
 
Unimatrix_001Author Commented:
Corrected, thanks for the pointer (no pun indended ;)), still getting the error though.
0
 
jkrCommented:
Are you sure the problem comes from *that* code? It does not seem so:

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
      return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
      return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
      ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW


///////////////////////////
class MyPoint{
    private:
         float x;
         float y;
    public:
         void setX(float a){x=a;}
         float getX(){return x;}
         void setY(float a){y=a;}
         float getY(){return y;}
};

class MyClass{
    private:
         MyPoint *pcpointer;
    public:
         void myMethod();
};


///////////////////////////

void MyClass::myMethod(){
    pcpointer=new MyPoint[6];

    for(int i=0;i<6;i++){
         pcpointer[i].setX(555);
         pcpointer[i].setY(777);
    }

    delete [] pcpointer;
}

void test () {

MyClass mc;

mc.myMethod();
}

void main () {

int tmpFlag;

tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;
tmpFlag |= _CRTDBG_ALLOC_MEM_DF;
tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;

// Set the new state for the flag
_CrtSetDbgFlag( tmpFlag );

test();

_CrtDumpMemoryLeaks ();
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Unimatrix_001Author Commented:
I also notice if I comment out:

         pcpointer[i].setX(555);
         pcpointer[i].setY(777);

It works fine, with the delete operator in also....
0
 
jkrCommented:
Have you tried passing the correct type (i.e. float)?

        pcpointer[i].setX(555.0);
        pcpointer[i].setY(777.0);
0
 
Unimatrix_001Author Commented:
No change...
0
 
jkrCommented:
Again - is this exactly the same code that causes these errors?
0
 
Unimatrix_001Author Commented:
The actual code is pretty much that but it reads the values in from a file as follows:

Instead of:
    for(int i=0;i<6;i++){
         pcpointer[i].setX(555);
         pcpointer[i].setY(777);
    }

It is:
      int Fcount=0;
      char buffer[256];
      ifstream filestream(myFile);
      if(!filestream.is_open())
            return;
      while(!filestream.eof()){
            filestream.getline (buffer,255);
            int c=0;
            char *p=strtok(buffer, ",");
            while(p!=NULL){
                  if(c==0) pcpointer[Fcount].setX(atof(p));
                  if(c==1) pcpointer[Fcount].setY(atof(p));
                  p=strtok (NULL, ",");
                  c++;
            }
            Fcount++;
            memset(buffer, '\0', sizeof(char)*255);
      }


I don't know what's causing the error...
0
 
jkrCommented:
And where's the 'new'  operation in the above? I can only see that you're accessing 'pcpointer[Fcount]'. If 'Fcount' grows larger than the number of elements in the array, there's a problem...
0
 
Unimatrix_001Author Commented:
Yep that was the prob, Fcount was being incremented too often. A silly mistake. Thanks jkr, enjoy the points. :)
0
 
jkrCommented:
Thanx :o)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now