Dynamically allocate and delete an array of objects in c++ -


so did research, , far have following (example code):

a class:

class my_class {   my_class(int a, double b);   ... } 

and dynamic array:

my_class **array_t; 

and initialization (with random numbers):

int amount = 10; array_t= new my_class*[amount];  (int = 0; < amount; i++) {    array_t[i] = new my_class(1, 2.0); } 

i think should work, right? @ least compiler not throwing errors @ me.

now change size of array_t. thought simplest way delete array, , allocate new memory it. tried this:

1: (int = 0; < amount; i++) { 2:   delete array_t[i]; 3: } 4: delete[] array_t; 

and in next step allocate new memory shown above.

however following error messages:

line 2: error: expected primary-expression before '[' token line 4: error: expected primary-expression before ';' token 

i read loop not necessary. anyway none of 2 delete statements work..

this might stupid error, don't know wrong in here :d appreciated!

try write delete [] array_t; in cycle. because in way you'll "delete" array. , reallocate use array_t= new my_class[number];


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -