empty array of objects

hi,

i’m trying to create an empty array of objects which adds a new item every time i click the mouse.
if i try something like this:

  
MyClass objects[] = new MyClass[50]  

it actually calls the constructor and creates the 50 objects, but that’s not what i want.

i tried

  
MyClass** objects = new MyClass*[50]  

but now when i click (add an object at pos 0) the application freezes.

  
  
if (i < 50) {  
  objects[i] = new MyClass;  
  i++;  
}  
  

not sure what to do next…

Hi!

Two things you can do.

1: Create a pointer to 50 objects - then create each object on mouse click

in testApp.h

  
MyClass * objects[50];   
int numObjects = 0;  

in testApp.cpp

  
  
void testApp::setup(){  
  numObjects = 0;  
}  
  
void testApp::mousePressed(int x, int y, int button){  
    if(numObjects >= 50)return;  
    objects[numObjects] = new MyClass();  
    numObjects++;  
}  

//with pointers to objects you need to use -> instead of .
//so objects[i]->myFunction();

Or the c++ vector way. With a resizable c++ array.

in testApp.h

  
  
#include <vector>  
  
vector <MyClass> objects;  
int numObjects = 0;  
  

in testApp.cpp

  
  
void testApp::setup(){  
  numObjects = 0;  
}  
  
void testApp::mousePressed(int x, int y, int button){  
     
   //add a new object to the end of our vector (c++ array)  
   //you can still reference each object like objects[i].myFunction();  
  
   objects.push_back( MyClass() );  
   numObjects = objects.size();  
}  

Hope that helps!
Theo

thanks theo, it worked

another question: if i use the pointer version, do i need to delete it in the destructor?

  
delete objects;  

also, let’s say i have a function that creates its own MyClass pointer:

  
  
void testApp::swap(MyClass* obj1, MyClass* obj2) {  
  MyClass* tmp;  
  if (obj1 > obj2) {  
    tmp = obj1;  
  } else {  
    tmp = obj2;  
  }   
  
  // do some more stuff...  
  
  // delete tmp; ?  
}  
  

do i need to delete this pointer at the end of the function code or is this taken care of automatically?

thanks

If you do:

  
delete objects  

you’re actually deleting the whole array but not the objects, so you’ll end with all that memory lost. You have to loop the whole array position by position and delete each object. something like:

  
for(int i=0;i<arrlen;i++){  
   delete objects[i];  
}  
delete objects;  
  

A general rule can be for each new you have in your code you must have a delete when you finish using that object.

For the second case, this same rule applies, you haven’t do a new for tmp, so if you do a delete you’re actually deleting obj1 or obj2.
Also usually you delete objects at the same level you created them, that way each function knows what it has created and what it has to delete.

One more for you:

If I have an array of arrays of pixels, for example

  
  
unsigned char** buffer;  
buffer = new unsigned char* [50]  
for (int i=0; i < 50; i++) {  
  buffer[i] = new unsigned char[640*480*3];      
}  
  

Would the following be the way to delete it?

  
  
for (int i=0; i < 50; i++) {  
  delete buffer[i];  
}  
delete buffer;  
  

And what’s the difference between

  
  
delete buffer;  
  

and

  
  
delete [] buffer;  
  

?

Thanks!

Another “delete” question:

How come the following works fine:

  
  
//--------------------------------------------------------------  
void testApp::useless(int w, int h) {  
    unsigned char* px;  
    px  = new unsigned char[w*h*3];  // RGB  
    for (int i = 0; i < w*h*3; i++) {  
        px[i] = 255;  
    }  
  
    delete [] px;  
}  
  

But the following gives me an error:

  
  
//--------------------------------------------------------------  
void testApp::useless(ofImage& img) {  
    unsigned char* px;  
    px  = new unsigned char[img.width*img.height*3];  // RGB  
    px = img.getPixels();  
  
    delete [] px;  
}  
      
  

  
  
openFrameworks(1645,0xa075c074) malloc: *** error for object 0x5841000: pointer being freed was not allocated  
  

re delete vs delete[]: when you go new[] (as opposed to just plain new) there’s a little bit of overhead allocated. delete[] will delete this overhead, plain delete won’t.

re ‘pointer being freed was not allocated’:

you’re allocating memory here:
px = new unsigned char[img.width*img.height*3]; // RGB
so px is a pointer to a fresh block of unsigned chars.

but then you overwrite px here:
px = img.getPixels();
so you’ve lost the pointer to your previously made fresh block - this is a memory leak - and you shouldn’t be trying to delete the pointer returned from img.getPixels().

hey damian, what’s up? (it’s elie, from visualizar)

just to be clear, for the second case, the

  
  
px  = new unsigned char[img.width*img.height*3];  
  

before the

  
  
img.getPixels();   
  

is unnecessary (and actually bad since it causes a memory leak), right?