Re: Memory leak

From: Alan Johnson (alanwj_at_mailandnews.com)
Date: 05/17/04


Date: Mon, 17 May 2004 15:21:18 -0500

John wrote:
> Hi Karl:
> Thank you very much. I understand now.
> On this discussion list, there are so many warm-hearted experts who
> spend their precious time instructing me--a newbie of C++.
> I have two questions. Is the following code free of trouble?
> void f()
> {
> X *x0 = new X(10);//create and initialize an object of class X.
> X *x1;
> x1 = x0;
> delete x1; //release the memory that x0 uses.
> }
>
> I do not "delete" x0, but "delete" x1 to release the memory. Will it
> cause any problem. An old code that I am modifying uses this approach.

No problems there. Although it isn't a particularly "safe" way to do
things, because code that is using x0 doesn't necessarily know that the
memory to which it points has been deleted.

If you need to use multiple pointers to point to the same dynamic
memory, try to come up with some scheme that let's you know which
pointer "owns" (i.e., is responsible for deleting) the memory. One
possible approach is as follows:

---
X *x0 = new X(10); // Allocate some memory. This memory is owned by x0.
X *x1 = NULL;  // This pointer doesn't own anything, so it is NULL.
f(x0); // Do something with x0.
x1 = x0;   // Transfer ownership of memory
x0 = NULL; // to x1.
f(x1); // Do something with x1.
if (x1) delete x1; // Delete the memory, but if and only if x1 owns it.
---
I'd mostly try to just avoid passing ownership of allocated memory 
blocks around in the first place, though.  Also, depending on what you 
are doing, you may try using an auto_ptr to "own" your allocated memory, 
so that you don't need to worry about whether or not it gets 
deallocated.  For example:
void f()
{
   auto_ptr<X> x0 = new X(10);
   auto_ptr<X> x1;
   // Do something with x0.
   x1 = x0 ;
   // Do something with x1.
} // Ignore the memory, it will get deleted on its own.
A nice auto_ptr tutorial is available here:
http://www.gotw.ca/publications/using_auto_ptr_effectively.htm
> 
> Another question is:
> What is the differece between the two lines below:
>      X *xx = new X;//line 1
>      X *xx1;//line 2
> Line 1 declare xx and allocate it memory.
> Line 2 declare xx1 and xx1 points to somewhere unknown. Has xx1 been
> allocated memory?
This would all depend on the platform and/or implementation, but for the 
sake of discussion, let's say that pointers, on your system, require 4 
bytes, and the object you are calling X requires 32 bytes.
Let's look at line 2 first.  This allocates 4 bytes of "automatic" 
memory.  By automatic, we mean that the any allocation and deallocation 
of the memory is handled for us by the compiler.  This is no different 
from declaring an int, double, char, etc.  The standard doesn't specify 
how this must be implemented, but typically this occurs on what is 
referred to as "the stack".  What is the value contained in those 4 
bytes?  We can't really say, because it wasn't initialized.
Now, line 1.  Obviously this line must do at least as much as line 2. It 
allocates 4 bytes (which happen to have the name xx) of automatic 
memory. It also allocates 32 bytes (at least) of dynamic memory. 
Dynamic memory means that the programmer is responsible for allocating 
and deallocating it, instead of the compiler.  In most implementations, 
this memory comes from a place called "the heap" or "the free store" 
(refer to other posts in this newsgroup for arguments about what to call 
this).  What is in the 4 bytes called xx?  If we look, we should find 
that the value there is the memory address at which our 32 byte block 
got allocated, which is, of course, why we call it a pointer.
> Thanks  a lot.
> 
> John
>   
Alan


Relevant Pages

  • Re: Explications for malloc ang some questions about pointers
    ... A pointer is a thing that contains the address of some ... block of memory somewhere. ... "malloc" allocates a number of bytes and returns a pointer to ... You have to go through the lists a_tmp and b_tmp, ...
    (comp.lang.c)
  • Re: "Mastering C Pointers"....
    ... > If all the structs are part of a single object (e.g., ... For each pointer, cast it to char* and subtract the ... > details of how mallocallocates memory. ...
    (comp.lang.c)
  • Re: compile
    ... which allocates memory on a garbage-collected heap. ... which identifies some pointer or GCMalloc'd structure as ... in compilation with dummy arguments so as to fake a call to a routine ...
    (comp.lang.lisp)
  • Re: segfault w/ block, but not file scope
    ... >> int main ... When you define this pointer a file scope, ... allocates the necessary memory. ...
    (comp.lang.c)
  • Re: Framework 2.0 array redim unsatisfactory performance
    ... implementation details of Redim, Redim Preserve, and List. ... significantly higher object allocated overhead then List. ... ReDim simply allocates a new ... but I do not even attempt to test its memory ...
    (microsoft.public.dotnet.languages.vb)