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: Finding memory leaks through ps command - comp.unix.solaris ...... any tools that work for Java since that's not compiled > code. Yes it is. Anyway, most implementations of Java have mark & sweep garbage collection, so memory leaks ... Matlab memory problems - comp.soft-sys.matlab> Hey Guys, *snip* > I have the following questions: > 1) Although I am pretty sure that my code does not leak memory (and I dont > think that is possible to do ... process memory usage - vsz and rss? - comp.unix.solarisAlso, if you have access to the source, a code inspection > might bring any memory leaks to light - although if it's a big > program, this could be quite a daunting task ... Annoying kernel memory leak in U6? - comp.unix.solarisI'm trying to figure out/find what I think might be a kernel memory leak in Solaris 10 ... Damn the living - It ... code from a uni IBM machine that had a point kernel ... detecting leaks with purify in shared libraries - comp.unix ...> > On my solaris 10 box, I have a propriety application (no source code), > which ... Finding memory leaks through ps command - comp.unix.solaris ... When the library function ... how to overcome "java.lang.OutOfMemory" Error - comp.lang.java ...A profiler can help identify where the leak is. So can really, really rigorous reasoning about the source code. Memory leaks in Java come from repeatedly keeping a ... writing robust software? - comp.lang.c++.moderated> - memory leaks, dangling pointers etc. Don't manually manage memory or other resources. Rather, code a policy fo= r when and how to release resources into special ... mexCallMATLAB : mxArray input and output problem - comp.soft-sys ...I can get the code to compile and transfer arrays of the correct size, but the ... Doing so simply creates a memory leak since the downstream mexCallMATLAB will wipe out ... allocating a previously allocated pointer - comp.lang.fortran ...You could have had a pointer to it in the code you "forgot" to show between the ... null() >> >> allocate(a(10)) >> allocate(a(20)) >> >> Other than the memory leak ... code speed moving from fortran 77 compiler to f2003 compiler ...Does anyone have experience with moving a large code set from a f77 compiler ... I predict that Fortran 9x will be less prone to memory leaks and will give you better ... segmentation fault when shared object using STL is statically ...If you link other code NB> with the library, you must provide complete object ... The only trouble with mismatch can come as a memory leak, if the allocator which has non ... Large Arrays - Memory Problems - comp.soft-sys.matlabDear All, I'm running into serious memory problems ... this is an easy, simple and flexible solution to code ... _only_ be done with unshared arrays to prevent memory leaks ... how to draw ER and UML diagram in eclipse? - comp.lang.java ...... to draw database ER diagram andUML diagrams?I've tried omondo years ago, it leaks memory ... Need to link shared objects after changing source code but not the ... how to ... nawk: out of space in tostring on ... - comp.lang.awk... Need to always set the first line as we are changing $0 down in the code $0 ... It really is a nawk (gawk) memory leak when using arrays and next, getline etc. libumem+mdb stack formatting - comp.unix.solarisFinding memory leaks through ps command - comp.unix.solaris ... libumem+mdb stack ... solaris Can I pipe the output of this command ... templates, running the code ... Memory leak - Wikipedia, the free encyclopediaIn object-oriented programming, a memory leak happens when an object is stored in memory but cannot be accessed by the running code. A memory leak has symptoms similar to a ... Memory Leak Detection in .NET - CodeProjectGarbage collection and memory leak detection in .NET.; Author: Deepak.Kumar; Updated: 21 Jul 2007; Section: .NET Framework; Chapter: Platforms, Frameworks & Libraries ... 7/26/2012 11:55:01 AM
|