c++ - Deleting array of class objects? -
it's common knowledge code below correctly frees memory of 100 integers.
int* ip = new int[100]; delete [] ip;
and think user defined classes works:
node* ip = new node[100]; delete [] ip;
in first case, size of memory freed (400 bytes), determined @ compile time? basically, goes on internally?
in second case, destructor of
node
called on each of 100 objects?
essentially, have been using syntax, never understood goes on internally , curious.
- no. memory allocator invisibly keeps track of size. size cannot determined @ compile time, because allocation not dynamic , following not work:
size_t n; std::cin >> n; = new int[n]; // interesting delete[] a;
- yes. convince of fact, try
struct foo { ~foo() { std::cout << "goodbye, cruel world.\n"; } }; // in main size_t n; std::cin >> n; foo *a = new foo[n]; delete[] a;
Comments
Post a Comment