Possible Duplicate:
delete[] an array of objects
The memory is allocated as follows:
struct foo {
int size;
int * arr;
};
(*structA).arr = new int[(*structA).size];
How does one deallocate it?
Possible Duplicate:
delete[] an array of objects
The memory is allocated as follows:
struct foo {
int size;
int * arr;
};
(*structA).arr = new int[(*structA).size];
How does one deallocate it?
Anything you allocate with new[]
must be deleted with delete[]
:
structA->arr = new int[structA->size];
...
delete[] structA->arr;
In this particular example, it would be better to use std::vector
instead. Let it handle the memory allocation and deallocation for you. You can use its size()
method to determine how many items it is holding:
struct foo {
std::vector<int> arr;
};
structA->arr.resize(some value here);
...
int size = structA->arr.size();
The memory allocated will be deleted when you call
delete[] (*structA).arr;
As for your struct, it depends on whether you allocated your struct on the heap or the stack.