Will this code leaks memory

  • Follow


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! ]

0
Reply rahul.ruikar (25) 5/10/2006 9:26:38 PM

LinuxGuy wrote:
> 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! ]

0
Reply Victor 5/11/2006 5:47:36 PM


LinuxGuy wrote:
> 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! ]

0
Reply Jeffrey 5/11/2006 5:52:26 PM

LinuxGuy wrote:

> 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! ]

0
Reply Eric 5/11/2006 10:18:37 PM

>>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! ]

0
Reply arunkg999 5/11/2006 10:19:31 PM

LinuxGuy wrote:
> Can someone please tell if following code leaks memory
>
> void someFunction()
> {
>
> char MyArray[512];
> 
> memset(MyArray,0,512);
> 
> }

There is no leakage of memory.


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

0
Reply dan2online 5/11/2006 10:19:52 PM

On 10 May 2006 17:26:38 -0400, "LinuxGuy" <rahul.ruikar@gmail.com>
wrote:

>Can someone please tell if following code leaks memory
>
>void someFunction()
>{
>
>char MyArray[512];
>
>memset(MyArray,0,512);
>
>}
>

No, there is no memory leak, as the array is defined within the
function as a local variable, and will completely disappear on exit.

BTW, if you will be using an initializaed array, woldn't it be a
perfect place to use a vector?

#include <vector>

void someFunction()
{
	std::vector<char> MyArray(512,'\0');
}

Regards,

Zara

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

0
Reply Zara 5/11/2006 10:27:31 PM

LinuxGuy wrote:
> Can someone please tell if following code leaks memory
> 
> void someFunction()
> {
> 
> char MyArray[512];
> 
> memset(MyArray,0,512);
> 
> }

No. The memory for the array is released as soon as the
control flow leaves the function. It is allocated from the
stack, not the heap.

So long,
	Thomas

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

0
Reply Thomas 5/11/2006 11:08:17 PM

LinuxGuy wrote:

> Can someone please tell if following code leaks memory
> 
> void someFunction()
> {
> 
> char MyArray[512];
> 
> memset(MyArray,0,512);
> 
> }

No it doesn't. MyArray has automatic storage; its memory will be 
reclaimed (popped off the stack) when it goes out of scope at the end of 
someFunction.


-- 
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address 
with "kapsch" and the top level domain part with "net".

The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.


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

0
Reply Gerhard 5/11/2006 11:08:41 PM

> Can someone please tell if following code leaks memory
> 
> void someFunction()
> {
> 
> char MyArray[512];
> 
> memset(MyArray,0,512);
> 
> }
> 

No.
What makes you think it could?

Wojtek



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

0
Reply Wojtek 5/11/2006 11:11:43 PM

>char MyArray[512];
>memset(MyArray,0,512);


As you are setting the memory of fixed length  512  array and you are
not allocating any thing, hence it will never leak.

Brijesh


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

0
Reply singh 5/11/2006 11:12:04 PM

No.


LinuxGuy wrote:
> 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! ]

0
Reply flagos 5/11/2006 11:15:33 PM

LinuxGuy:

There is no leaks in your code.

PD. Sory for the last post.


LinuxGuy wrote:
> 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! ]

0
Reply flagos 5/11/2006 11:17:31 PM

"LinuxGuy" <rahul.ruikar@gmail.com> writes:

> 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! ]

0
Reply Thomas 5/11/2006 11:31:09 PM

13 Replies
107 Views

(page loaded in 0.17 seconds)

Similiar Articles:


















7/26/2012 11:55:01 AM


Reply: