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 

Will this code leaks memory

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





PostPosted: Wed May 10, 2006 10:21 pm    Post subject: Will this code leaks memory Reply with quote



Can someone please tell if following code leaks memory

void someFunction()
{

char MyArray[512];

memset(MyArray,0,512);

}


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





PostPosted: Thu May 11, 2006 6:21 pm    Post subject: Re: Will this code leaks memory Reply with quote



LinuxGuy wrote:
Quote:
Can someone please tell if following code leaks memory

void someFunction()
{

char MyArray[512];

memset(MyArray,0,512);

}

No, it doesn't seem to. An easier way to make the array 0 would be
to initialise it

char MyArray[512] = {0};

(yes, I know, the '0' in those curly braces is not necessary, I just
find it a bit more readable).


V
--
Please remove capital As from my address when replying by mail



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





PostPosted: Thu May 11, 2006 6:21 pm    Post subject: Re: Will this code leaks memory Reply with quote



LinuxGuy wrote:
Quote:
Can someone please tell if following code leaks memory

void someFunction()
{
char MyArray[512];
memset(MyArray,0,512);
}


It shouldn't, unless you're doing something funky elsewhere. I can't
say I like the hard-coded 512, though. Why would this be leaky?

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





PostPosted: Thu May 11, 2006 10:21 pm    Post subject: Re: Will this code leaks memory Reply with quote

LinuxGuy wrote:

Quote:
Can someone please tell if following code leaks memory

void someFunction()
{

char MyArray[512];

memset(MyArray,0,512);

}

No it wont, why did you think it might?
Eric


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





PostPosted: Thu May 11, 2006 10:21 pm    Post subject: Re: Will this code leaks memory Reply with quote

Quote:
void someFunction()
{
char MyArray[512];
memset(MyArray,0,512);
}

This piece of code wont leak memory.
what you have done is made an automatic array fo 512 char and then set
0 into it. Once you get out of the scope, MyArray will get destroyed
automatically. Hence no leaks


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





PostPosted: Fri May 12, 2006 12:21 am    Post subject: Re: Will this code leaks memory Reply with quote

"LinuxGuy" <rahul.ruikar (AT) gmail (DOT) com> writes:

Quote:
Can someone please tell if following code leaks memory

Why do you think that it does?


[ 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.