empty array of objects


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;  

not sure what to do next…


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();  

//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!

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?


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;  


delete [] buffer;  



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


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

Hi, quite an old thread, but gave me an idea of what I’m trying to achieve, is there a way to create the array of objects with smart pointers?
I was trying:

unique_ptr<Object>* objects[numObjects];

in cpp:

for (int i = 0; i < numObjects; ++i)

hello! std::make_unique (and often std::make_shared) are indeed the way to go as far as instantiation goes. so your idea close, you just need to hold onto the pointers somewhere as the objects are made.

std::array is the closest equivalence to a conventional array:

std::array<std::unique_ptr<Object>, numObjects> objects;

for (int i = 0; i < objects_array.size(); ++i) {
   objects[i] = std::make_unique<Object>();

note that with the STL containers you can use this form to iterate:

// draw()
for (const auto & object: objects) {

you could also use std::vector which is pretty convenient as it is dynamic (i.e. an array that can grow and shrink size over time).


1 Like