deallocating memory with delete

Code snipet:
class foo {
public:
         foo() {var=new double [1000];}
         ~foo() {delete [] var;}
         double * var;
};
int main() {
         foo *a;
         a = new foo;
         delete [] foo;
         return 0;
}

I know that the memory allocated to the pointer "a" is released by delete, but what about "var" does the compile automatically know to release it since it is inside the class or do I need to do it.
BigLobowskiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddunleaCommented:
Hi BigLobowski,

You need to change "delete[] foo" to "delete foo" as it was created with "new" and not "new ... [...]"

When you delete a, it calls the destructor of a which in turn deletes var.
0
AaronReamsCommented:
hi there,

you need to delete the object not the class.

delete [] foo;

should be

delete a;

also, when you delete "a", the destructor of "a" will be called before "a" is deallocated.  that is where you need to delete "var", as you have correctly.

cheers,
aaron
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddunleaCommented:
Oops. Should've spotted that bit. It's clearly past my bedtime.
0
AaronReamsCommented:
no worries.  those late nights will get the best of anyone.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.