| View previous topic :: View next topic |
| Author |
Message |
LinuxGuy Guest
|
Posted: Wed May 10, 2006 10:21 pm Post subject: Will this code leaks memory |
|
|
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
|
Posted: Thu May 11, 2006 6:21 pm Post subject: Re: Will this code leaks memory |
|
|
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
|
Posted: Thu May 11, 2006 6:21 pm Post subject: Re: Will this code leaks memory |
|
|
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
|
Posted: Thu May 11, 2006 10:21 pm Post subject: Re: Will this code leaks memory |
|
|
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
|
Posted: Thu May 11, 2006 10:21 pm Post subject: Re: Will this code leaks memory |
|
|
| 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
|
Posted: Fri May 12, 2006 12:21 am Post subject: Re: Will this code leaks memory |
|
|
"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 |
|
 |
|