C++ Returning values from dynamic array (image class) -


i have problem following code (compiler not complaining error message @ runtime - r6010 abort). have created image class reads data image , stores in dynamically allocated array. want pass image data anther array in int main. reason not work.

class image { private:     char* charimage;     int totrows;     int totcol;     int size;     int maxval;     char magic[2];  public:     image();     image (const image& orig);     ~image();     void operator=(const image&); //overloaded assignment operator     void readimage();     char returnimage(int i);     int getsize(); };  image::image()//constructor {     size = (3 * totrows * totcol);     charimage = new char [size]; }  image::image (const image& orig)//copy constructor {     totrows = orig.totrows;     totcol = orig.totcol;     size = orig.size;     charimage = new char [size]; }  image::~image()//destructor {      delete []charimage; }  void image::operator=(const image& orig) {     totrows = orig.totrows;     totcol = orig.totcol;     size = orig.size;     charimage = new char [size];      (int = 0; < size; i++)     {         charimage[i]=orig.charimage[i];     } }  void image::readimage() {     //opening original image     ifstream oldimage;     oldimage.open ("image2.ppm", ios::in | ios::binary);      if (!oldimage)         cout << "\nerror: cannot open image file! " << endl;      //reading header of original image file     oldimage >> magic [0] >> magic [1];      //if image not in right format, not proceed!     if ((magic [0] != 'p')||(magic [1] != '6'))         cout << "\nerror: image in wrong format!" << endl;      else         oldimage >> totrows >> totcol >> maxval;      //reading image in binary format , storing in array of characters     oldimage.read(charimage, size);  }  char image::returnimage(int i) {     return charimage[i]; } int image::getsize() {     return size; }  int main () {     char* charimage;     int size;     image myimage;     myimage.readimage();     size = myimage.getsize();      charimage= new char [size];      (int i=0; i<size; i++)     {         charimage[i]=myimage.returnimage(i);     }      delete [] charimage;      return 0; } 

one obvious error don't set dimensions of image int default constructor:

image::image()//constructor {     size = (3 * totrows * totcol);     charimage = new char [size]; } 

here, totrows , totcol uninitialized, , have value.

then, in assignment operator, not deallocate array pointed @ charimage before making point new array, leaking resources.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -