470,848 Members | 1,822 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

Will this code leaks memory

Can someone please tell if following code leaks memory

void someFunction()
{

char MyArray[512];

memset(MyArray,0,512);

}

May 10 '06 #1
5 1266
No - it can't leak. However it is better to write
memset(MyArray,0,sizeof(MyArray));
to avoid duplicating of magic number (512).

May 10 '06 #2
* Dervish:
[non-quoting]
Please read the FAQ and the monthly welcome message.

Please quote what you're replying to.

This isn't a web forum or instant messaging.

No - it can't leak. However it is better to write
memset(MyArray,0,sizeof(MyArray));
to avoid duplicating of magic number (512).


memset is not type safe and thus should be avoided where practically
possible.

In this case, if a raw array is really needed (it seldom is), write

void someFunction()
{
char MyArray[512] = {};
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 10 '06 #3
even if I don't use memset() and just declare the array then will it be
deallocated when function exits..

May 10 '06 #4
LinuxGuy wrote:
even if I don't use memset() and just declare the array then will it be
deallocated when function exits..


First, to reiterate: please QUOTE what you are replying to. Not
everyone is using Google Groups, and it helps all to follow the
conversation. (To quote automatically from GG, click "show options" and
then "reply" in the revealed header.)

Second, you seem to have either asked a question but omitted to proper
punctuation or inappropriately phrased an indicative sentence as an
interrogative. In either case, the answer is that, yes, automatic
variables and arrays are freed when they leave scope.

Cheers! --M

May 10 '06 #5
LinuxGuy <ra**********@gmail.com> wrote:
even if I don't use memset() and just declare the array then will it be
deallocated when function exits..


Your failure to post properly has already been duly noted.

In answer to your actual question, yes, your array will be deallocated
when the function exits, no matter what you do to it. If your
implementation uses a stack (it probably does), it may help to know
that space allocated on the stack is *always* freed when the function
returns. Such is the beauty of "automatic" (whatever C++ calls it)
storage.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Maurice | last post: by
2 posts views Thread by Mike Krasnik | last post: by
9 posts views Thread by Frank1213 | last post: by
10 posts views Thread by darkStar_e2 | last post: by
2 posts views Thread by asterixgallier | last post: by
7 posts views Thread by Ragnar Agustsson | last post: by
20 posts views Thread by gNash | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.