|
From: guojing1982423 on 10 Nov 2006 13:57 Hi, any body can tell me what's wrong with following code? Is there memory leak occured? class ABC { char *p; public: ABC() { p = (char*)malloc(50); } ~ABC() { free(p); } }; void main() { ABC *pabc = new ABC; ABC abc; delete pabc; } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Joshua Lehrer on 10 Nov 2006 17:04 { Quoting a little more context would have been helpful. -mod } "What's wrong with it" and "is there a memory leak" are two different questions. You will get a lot of people telling you what is "wrong" with the code. As for checking for a memory leak, no, there is no memory explicitly allocated by this program that is not also explicitly released. The constructor allocates 'p' and the destructor frees it. Your main allocates one object on the heap and one on the stack, and it properly deallocates the one on the heap. As for what is wrong, I'm quite sure that others will list them for you. joshua lehrer http://www.lehrerfamily.com/ -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: John L Fjellstad on 10 Nov 2006 19:02 guojing1982423(a)yahoo.com.cn writes: > Hi, any body can tell me what's wrong with following code? Is there > memory leak occured? > class ABC > { > char *p; > public: > ABC() > { > p = (char*)malloc(50); > } > ~ABC() > { > free(p); > } > }; > void main() > { > ABC *pabc = new ABC; > ABC abc; > delete pabc; > } main returns an int like this int main() otherwise, i can't see anything particularly wrong with the code. -- John L. Fjellstad web: http://www.fjellstad.org/ Quis custodiet ipsos custodes Replace YEAR with current four digit year [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: bestbrain on 11 Nov 2006 03:31 guojing1982423(a)yahoo.com.cn wrote: > Hi, any body can tell me what's wrong with following code? Is there > memory leak occured? > class ABC > { > char *p; > public: > ABC() > { > p = (char*)malloc(50); > } > ~ABC() > { > free(p); > } > }; > void main() > { > ABC *pabc = new ABC; > ABC abc; > delete pabc; > } Apart from usal stuff like operatr= . I wont exactly call it a memory leak. may be FMR or FMW and stuff in purify terms. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ivan Novick on 12 Nov 2006 01:32
> Hi, any body can tell me what's wrong with following code? Is there > memory leak occured? > class ABC > { > char *p; > public: > ABC() > { > p = (char*)malloc(50); > } > ~ABC() > { > free(p); > }}; If a compiler generated copy constructor or assignment operator is called then you will not do the malloc, but you will still try to do the free. You can declare your own assignment operator and copy constructor to make sure they leave an object in a consistent state. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |