It's almost common knowledge that the code below correctly frees the memory of 100 integers.
int* ip = new int[100];
delete [] ip;
And I think even for user defined classes it works:
Node* ip = new Node[100];
delete [] ip;
In the first case, is the size of memory to be freed (400 bytes), determined at compile time? Basically, what goes on internally?
In the second case, will the destructor of Node
be called on each of the 100 objects?
Essentially, I have been using this syntax, but never understood what goes on internally and now I am curious.
size_t n;
std::cin >> n;
a = new int[n];
// do something interesting
delete[] a;
struct Foo {
~Foo() { std::cout << "Goodbye, cruel world.\n"; }
};
// in main
size_t n;
std::cin >> n;
Foo *a = new Foo[n];
delete[] a;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With