C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What's wrong with this snippet?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Nov 11, 2006 12:57 am    Post subject: What's wrong with this snippet? Reply with quote



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! ]
Back to top
Joshua Lehrer
Guest





PostPosted: Sat Nov 11, 2006 4:04 am    Post subject: Re: What's wrong with this snippet? Reply with quote



{ 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! ]
Back to top
John L Fjellstad
Guest





PostPosted: Sat Nov 11, 2006 6:02 am    Post subject: Re: What's wrong with this snippet? Reply with quote



guojing1982423 (AT) yahoo (DOT) com.cn writes:

Quote:
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! ]
Back to top
Guest






PostPosted: Sat Nov 11, 2006 10:10 am    Post subject: Re: What's wrong with this snippet? Reply with quote

guojing1982423 (AT) yahoo (DOT) com.cn wrote:
Quote:
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! ]
Back to top
Ivan Novick
Guest





PostPosted: Sun Nov 12, 2006 10:10 am    Post subject: Re: What's wrong with this snippet? Reply with quote

Quote:
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! ]
Back to top
Jiang
Guest





PostPosted: Sun Nov 12, 2006 9:48 pm    Post subject: Re: What's wrong with this snippet? Reply with quote

John L Fjellstad wrote:
Quote:
guojing1982423 (AT) yahoo (DOT) com.cn writes:


[snip]

Quote:

main returns an int like this
int main()

otherwise, i can't see anything particularly wrong with the code.


Using malloc requires the standard include file, of course this is
maybe just a typo.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
amit
Guest





PostPosted: Wed Nov 15, 2006 6:07 am    Post subject: Re: What's wrong with this snippet? Reply with quote

In the above program, nothing in main could call the copy construtor or
the assign operator. Surely we should write our own if we change the
program to make use of it.

~ amit

Ivan Novick wrote:
Quote:
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! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.