Say I have the following classes:
class A {};
Class C {
private:
A a[10];
};
int main() {
C c;
}
Will this code cause a memory leak? As in, will the default destructor that the compiler will define for the C class free the memory of the array of A objects successfully?
I tried to check myself but I couldn’t figure out how to run valgrind on OSX 10.7…
This will not leak. If you want to make sure, put a debug statement in the destructor of
A.EDIT: simple example…
Now compiling and running the above example, should produce 10 instances of the first debug statement and 10 instances of the second debug statement. The last 10 statements are produced when the 10 instances of
Aare destroyed (which is done automatically for you – hence the term automatic storage, when they go out of scope – in this case, that is when the instance ofC[which owns them] goes out of scope [at the end ofmain].)