Memory leak

Discussion in 'C++' started by John, May 16, 2004.

  1. John

    John Guest

    Hi all:

    When I run my code, I find that the memory that the code uses keeps
    increasing.
    I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    memory by the time it finishes execution. But I do not think it needs
    so much memory. About 500M memory should be enough. I have following
    questions about memory leak.
    (1).If in my code I only define constructor for my class, and do not
    define destructor, will it cause memory leak?
    (2).If in my code I only use "new" to declare new object, and do not
    use "delete", will it cause memory leak?

    For example, in the following code:

    void class1::function1()
    {
    class2 *r1;
    class2 *r2 = new class2;
    class2 *rr[20];

    ......

    function2(rr);

    ......
    //r1 and r2 are also used in function1().

    }

    In the above code, I have two classes and I define constructor for the
    two classes and do not define destructor. In function1(), I declare
    two pointers of class2 and an array of pointer of class2. The array rr
    is used to bring back values from function2(). For r2, I do not use
    "delete".
    Will r1, r2 and the array rr[] cause memory leak?
    The two pointers, r1 and r2, and array rr[] are local variables, when
    the code exits function1(), these local variables should be released
    automatically.
    Am I right?

    Thanks a lot.

    John
    John, May 16, 2004
    #1
    1. Advertising

  2. "John" <> wrote in message
    news:...
    > Hi all:
    >
    > When I run my code, I find that the memory that the code uses keeps
    > increasing.
    > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > memory by the time it finishes execution. But I do not think it needs
    > so much memory. About 500M memory should be enough. I have following
    > questions about memory leak.
    > (1).If in my code I only define constructor for my class, and do not
    > define destructor, will it cause memory leak?


    Depends on the class.

    > (2).If in my code I only use "new" to declare new object, and do not
    > use "delete", will it cause memory leak?


    Yes.

    Its a very simple rule, nothing to do with constructors or destructors. When
    your program runs every new allocates some memory, if you don't do a delete
    for the same memory then you have a memory leak.


    >
    > For example, in the following code:
    >
    > void class1::function1()
    > {
    > class2 *r1;
    > class2 *r2 = new class2;
    > class2 *rr[20];
    >
    > ......
    >
    > function2(rr);
    >
    > ......
    > //r1 and r2 are also used in function1().
    >
    > }
    >
    > In the above code, I have two classes and I define constructor for the
    > two classes and do not define destructor.


    That's irrelevant.

    > In function1(), I declare
    > two pointers of class2 and an array of pointer of class2. The array rr
    > is used to bring back values from function2(). For r2, I do not use
    > "delete".
    > Will r1, r2 and the array rr[] cause memory leak?


    Where are the deletes? There are no deletes so there are memory leaks all
    over the place.

    > The two pointers, r1 and r2, and array rr[] are local variables, when
    > the code exits function1(), these local variables should be released
    > automatically.
    > Am I right?


    Wrong. The variables destructed and the memory they occupy is 'released',
    BUT the memory they might be pointing to is not released.

    void f()
    {
    X x;
    X* xp = new X();
    ...
    }

    When you get to the end of this function, the memory for x and xp are both
    released. That has nothing to do with the memory pointed to by xp, which is
    a completely different thing. The memory for a pointer and the memory that
    it points to are not the same thing.

    It's very simple, every new must be matched by a delete.

    john
    John Harrison, May 16, 2004
    #2
    1. Advertising

  3. "John" <> wrote in message
    news:...
    > Hi all:
    >
    > When I run my code, I find that the memory that the code uses keeps
    > increasing.
    > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > memory by the time it finishes execution. But I do not think it needs
    > so much memory. About 500M memory should be enough. I have following
    > questions about memory leak.
    > (1).If in my code I only define constructor for my class, and do not
    > define destructor, will it cause memory leak?
    > (2).If in my code I only use "new" to declare new object, and do not
    > use "delete", will it cause memory leak?
    >
    > For example, in the following code:
    >
    > void class1::function1()
    > {
    > class2 *r1;
    > class2 *r2 = new class2;
    > class2 *rr[20];
    >
    > ......
    >
    > function2(rr);
    >
    > ......
    > //r1 and r2 are also used in function1().
    >
    > }
    >
    > In the above code, I have two classes and I define constructor for the
    > two classes and do not define destructor. In function1(), I declare
    > two pointers of class2 and an array of pointer of class2. The array rr
    > is used to bring back values from function2(). For r2, I do not use
    > "delete".
    > Will r1, r2 and the array rr[] cause memory leak?
    > The two pointers, r1 and r2, and array rr[] are local variables, when
    > the code exits function1(), these local variables should be released
    > automatically.
    > Am I right?
    >
    > Thanks a lot.
    >
    > John

    Hi,
    check out "auto_ptr" from any standard c++ boooks.

    kutty
    Kutty Banerjee, May 16, 2004
    #3
  4. John

    Ian Guest

    John wrote:
    > Hi all:
    >
    > When I run my code, I find that the memory that the code uses keeps
    > increasing.
    > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > memory by the time it finishes execution. But I do not think it needs
    > so much memory. About 500M memory should be enough. I have following
    > questions about memory leak.
    > (1).If in my code I only define constructor for my class, and do not
    > define destructor, will it cause memory leak?


    See next answer!
    > (2).If in my code I only use "new" to declare new object, and do not
    > use "delete", will it cause memory leak?
    >

    Yes, without a destructor, how is the memory you allocated in the
    destructor freed?

    An alternative is to use std::auto_ptr rather than a plain pointer.

    As a rule, any class with pointers must have a destructor and copy
    constructor (if nothing else, this makes you think about ownership of
    the data you reference through a pointer).

    Ian

    > For example, in the following code:
    >
    > void class1::function1()
    > {
    > class2 *r1;
    > class2 *r2 = new class2;
    > class2 *rr[20];
    >
    > ......
    >
    > function2(rr);
    >
    > ......
    > //r1 and r2 are also used in function1().
    >
    > }
    >
    > In the above code, I have two classes and I define constructor for the
    > two classes and do not define destructor. In function1(), I declare
    > two pointers of class2 and an array of pointer of class2. The array rr
    > is used to bring back values from function2(). For r2, I do not use
    > "delete".
    > Will r1, r2 and the array rr[] cause memory leak?
    > The two pointers, r1 and r2, and array rr[] are local variables, when
    > the code exits function1(), these local variables should be released
    > automatically.
    > Am I right?
    >
    > Thanks a lot.
    >
    > John
    Ian, May 16, 2004
    #4
  5. "Ian" <> wrote in message
    news:...
    > John wrote:
    > > Hi all:
    > >
    > > When I run my code, I find that the memory that the code uses keeps
    > > increasing.
    > > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > > memory by the time it finishes execution. But I do not think it needs
    > > so much memory. About 500M memory should be enough. I have following
    > > questions about memory leak.
    > > (1).If in my code I only define constructor for my class, and do not
    > > define destructor, will it cause memory leak?

    >
    > See next answer!
    > > (2).If in my code I only use "new" to declare new object, and do not
    > > use "delete", will it cause memory leak?
    > >

    > Yes, without a destructor, how is the memory you allocated in the
    > destructor freed?
    >
    > An alternative is to use std::auto_ptr rather than a plain pointer.
    >
    > As a rule, any class with pointers must have a destructor and copy
    > constructor (if nothing else, this makes you think about ownership of
    > the data you reference through a pointer).
    >
    > Ian
    >


    I think the OP is not talking about a class with pointers but a pointer to a
    class. Certainly that is what his code shows.

    In any case the rule is every new must be matched with a delete.
    Constructors and destructors are just a useful way of making sure that this
    rule is followed.

    Since the OP is clearly struggling with pointers I would advise avoid using
    new where possible. It's certainly a common newbie trait to use new where it
    isn't necessary.

    // newbie style coding
    void func()
    {
    X* xp = new X;
    x->some_func();
    delete x;
    }

    // simpler, safer and better coding
    void func()
    {
    X x;
    x.some_func();
    }

    There's nothing in the code that John posted that indicated he must use new
    at all.

    john
    John Harrison, May 16, 2004
    #5
  6. John

    Rolf Magnus Guest

    John wrote:

    > Hi all:
    >
    > When I run my code, I find that the memory that the code uses keeps
    > increasing.
    > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > memory by the time it finishes execution. But I do not think it needs
    > so much memory. About 500M memory should be enough. I have following
    > questions about memory leak.
    > (1).If in my code I only define constructor for my class, and do not
    > define destructor, will it cause memory leak?


    That depends on what the class does. If it allocates any resources, it
    should have a destructor that deallocates them.

    > (2).If in my code I only use "new" to declare new object, and do not
    > use "delete", will it cause memory leak?


    Yes. Everything you got from new should be deleted as soon as you don't
    need it anymore.

    > For example, in the following code:
    >
    > void class1::function1()
    > {
    > class2 *r1;
    > class2 *r2 = new class2;
    > class2 *rr[20];
    >
    > ......
    >
    > function2(rr);
    >
    > ......
    > //r1 and r2 are also used in function1().
    >
    > }
    >
    > In the above code, I have two classes and I define constructor for the
    > two classes and do not define destructor. In function1(), I declare
    > two pointers of class2 and an array of pointer of class2. The array rr
    > is used to bring back values from function2(). For r2, I do not use
    > "delete".
    > Will r1, r2 and the array rr[] cause memory leak?


    If you don't delete them, yes. However, since the pointers are local
    variables within your function and not member variables of your class,
    you don't need a destructor for cleaning them up, but rather would
    delete them in the function. Do you actually need them to be pointers
    anyway? If a direct instance suffices, use it.

    > The two pointers, r1 and r2, and array rr[] are local variables, when
    > the code exits function1(), these local variables should be released
    > automatically.


    The local variables are, but not the objects they point to.

    > Am I right?


    What you wrote is right, but I don't think it's what you meant :)
    Rolf Magnus, May 16, 2004
    #6
  7. John

    Anil Mamede Guest

    John wrote:

    > Hi all:
    >
    > When I run my code, I find that the memory that the code uses keeps
    > increasing.
    > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > memory by the time it finishes execution. But I do not think it needs
    > so much memory. About 500M memory should be enough. I have following


    Maybe you're creating objects inside a infinit while or for loop.

    Anil Mamede
    Anil Mamede, May 16, 2004
    #7
  8. John

    JKop Guest

    Where you use "new", you must use "delete".

    Where you use "new", you must use "delete".

    Where you use "new", you must use "delete".


    class2* r2 = new class2;

    delete r2;



    I highly suggest that you keep the asterisk beside THE CLASS NAME rather
    than beside the variable name.

    Think of "delete" as a function that takes one paramater, a pointer. You
    pass it the value of the variable "r2", which is of type "class2*".

    delete r2;


    ---


    As for normal run-of-the-mill variables, they come into being at the
    beginning of the block of code and are destroyed at the end of the block of
    code:


    int main(void)
    { //Right here, a comes into being

    class2 a;

    { //Right here, b comes into being

    class2 b;

    } // Right here, b is destroyed.


    return 0;

    } //Right here, a is destroyed
    JKop, May 16, 2004
    #8
  9. John

    Rolf Magnus Guest

    JKop wrote:

    > Think of "delete" as a function that takes one paramater, a pointer.
    > You pass it the value of the variable "r2", which is of type
    > "class2*".
    >
    > delete r2;


    I'd rather say think of new as a source and delete as a sink. Everything
    that came from 'new' has to go to 'delete'.

    > As for normal run-of-the-mill variables, they come into being at the
    > beginning of the block


    No, they start existing where they are defined.

    > of code and are destroyed at the end of the
    > block of code:
    >
    >
    > int main(void)
    > { //Right here, a comes into being


    Nope. a doesn't exist yet.

    > class2 a;


    Here, a commes into existance.

    >
    > { //Right here, b comes into being


    No. Same as a

    >
    > class2 b;
    > } // Right here, b is destroyed.
    >
    >
    > return 0;
    >
    > } //Right here, a is destroyed
    Rolf Magnus, May 16, 2004
    #9
  10. John

    John Guest

    "John Harrison" <> wrote in message news:<>...
    > "Ian" <> wrote in message
    > news:...
    > > John wrote:
    > > > Hi all:
    > > >
    > > > When I run my code, I find that the memory that the code uses keeps
    > > > increasing.
    > > > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > > > memory by the time it finishes execution. But I do not think it needs
    > > > so much memory. About 500M memory should be enough. I have following
    > > > questions about memory leak.
    > > > (1).If in my code I only define constructor for my class, and do not
    > > > define destructor, will it cause memory leak?

    > >
    > > See next answer!
    > > > (2).If in my code I only use "new" to declare new object, and do not
    > > > use "delete", will it cause memory leak?
    > > >

    > > Yes, without a destructor, how is the memory you allocated in the
    > > destructor freed?
    > >
    > > An alternative is to use std::auto_ptr rather than a plain pointer.
    > >
    > > As a rule, any class with pointers must have a destructor and copy
    > > constructor (if nothing else, this makes you think about ownership of
    > > the data you reference through a pointer).
    > >
    > > Ian
    > >

    >
    > I think the OP is not talking about a class with pointers but a pointer to a
    > class. Certainly that is what his code shows.
    >
    > In any case the rule is every new must be matched with a delete.
    > Constructors and destructors are just a useful way of making sure that this
    > rule is followed.
    >
    > Since the OP is clearly struggling with pointers I would advise avoid using
    > new where possible. It's certainly a common newbie trait to use new where it
    > isn't necessary.
    >
    > // newbie style coding
    > void func()
    > {
    > X* xp = new X;
    > x->some_func();
    > delete x;
    > }
    >
    > // simpler, safer and better coding
    > void func()
    > {
    > X x;
    > x.some_func();
    > }
    >
    > There's nothing in the code that John posted that indicated he must use new
    > at all.
    >
    > john


    Hi John,

    Thanks a lot.
    My code is based on an old code. I modify it and add my own functions.
    The old code uses linked list of objects everywhere. The linked list
    is implemented by using pointer. This is the reason why I must use
    pointer to a class.
    If I do not use "new", will it cause memory leak?
    For example,

    void func()
    {
    int *p = 0; // Initialize p to be 0.
    int a = 10;
    X *x; //line 1
    x->some_func();
    p = & a;
    }

    Will the above code cause memory leak?
    When line 1 is executed, the constructor of X will initialize x. By
    the end of func(), x is released. Am I right?

    Thanks again.

    John
    John, May 16, 2004
    #10
  11. "John" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<>...
    > > "Ian" <> wrote in message
    > > news:...
    > > > John wrote:
    > > > > Hi all:
    > > > >
    > > > > When I run my code, I find that the memory that the code uses keeps
    > > > > increasing.
    > > > > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > > > > memory by the time it finishes execution. But I do not think it

    needs
    > > > > so much memory. About 500M memory should be enough. I have following
    > > > > questions about memory leak.
    > > > > (1).If in my code I only define constructor for my class, and do not
    > > > > define destructor, will it cause memory leak?
    > > >
    > > > See next answer!
    > > > > (2).If in my code I only use "new" to declare new object, and do not
    > > > > use "delete", will it cause memory leak?
    > > > >
    > > > Yes, without a destructor, how is the memory you allocated in the
    > > > destructor freed?
    > > >
    > > > An alternative is to use std::auto_ptr rather than a plain pointer.
    > > >
    > > > As a rule, any class with pointers must have a destructor and copy
    > > > constructor (if nothing else, this makes you think about ownership of
    > > > the data you reference through a pointer).
    > > >
    > > > Ian
    > > >

    > >
    > > I think the OP is not talking about a class with pointers but a pointer

    to a
    > > class. Certainly that is what his code shows.
    > >
    > > In any case the rule is every new must be matched with a delete.
    > > Constructors and destructors are just a useful way of making sure that

    this
    > > rule is followed.
    > >
    > > Since the OP is clearly struggling with pointers I would advise avoid

    using
    > > new where possible. It's certainly a common newbie trait to use new

    where it
    > > isn't necessary.
    > >
    > > // newbie style coding
    > > void func()
    > > {
    > > X* xp = new X;
    > > x->some_func();
    > > delete x;
    > > }
    > >
    > > // simpler, safer and better coding
    > > void func()
    > > {
    > > X x;
    > > x.some_func();
    > > }
    > >
    > > There's nothing in the code that John posted that indicated he must use

    new
    > > at all.
    > >
    > > john

    >
    > Hi John,
    >
    > Thanks a lot.
    > My code is based on an old code. I modify it and add my own functions.
    > The old code uses linked list of objects everywhere. The linked list
    > is implemented by using pointer. This is the reason why I must use
    > pointer to a class.
    > If I do not use "new", will it cause memory leak?
    > For example,
    >
    > void func()
    > {
    > int *p = 0; // Initialize p to be 0.
    > int a = 10;
    > X *x; //line 1
    > x->some_func();
    > p = & a;
    > }
    >
    > Will the above code cause memory leak?


    No, the above code will crash, x is an uninitalised pointer.

    > When line 1 is executed, the constructor of X will initialize x.


    No, pointers do not have constructors, x is a pointer.

    > By
    > the end of func(), x is released. Am I right?
    >


    You seem hung up on pointers, often you don't have to use them. Here is the
    above program rewritten so that it doesn't use a pointer.

    void func()
    {
    int *p = 0; // Initialize p to be 0.
    int a = 10;
    X x; // not a pointer
    x.some_func(); // no need for a pointer
    p = & a;
    }

    Now because x is not a pointer, there are no problems.

    Do not use pointers unless you are sure that you really need them. Linked
    lists are one example of where you do need to use pointers, but as you are
    finding out using pointers is tricky. If you can I would switch to C++'s
    built in linked list class. That should cure your memory leak problems.

    john
    John Harrison, May 16, 2004
    #11
  12. John

    Rolf Magnus Guest

    John wrote:

    > My code is based on an old code. I modify it and add my own functions.
    > The old code uses linked list of objects everywhere. The linked list
    > is implemented by using pointer. This is the reason why I must use
    > pointer to a class.
    > If I do not use "new", will it cause memory leak?


    No.

    > For example,
    >
    > void func()
    > {
    > int *p = 0; // Initialize p to be 0.
    > int a = 10;
    > X *x; //line 1
    > x->some_func();
    > p = & a;
    > }
    >
    > Will the above code cause memory leak?


    You can't call some_func() on x, because it points into the desert. You
    must first let x point to an object before you can use it.

    > When line 1 is executed, the constructor of X will initialize x.


    No, it won't. You seem to be very confused about objects vs. pointers,
    construction vs. memory allocation and about the lifetime of object
    created in several different ways. I suggest getting a good book about
    C++ that explains those concepts in depth. You won't do yourself any
    favor if you stick to this trial/error method.

    > By the end of func(), x is released. Am I right?


    Again, x is a pointer. The memory that x (i.e. the pointer) uses will be
    freed, nothing more.
    Further, the lifetime of an object is independant of whether a pointer
    points to it or not, so in your above example, the memory that a takes
    will be released when the function returns, but that doesn't have
    anything to do with the existance of p.
    Rolf Magnus, May 16, 2004
    #12
  13. John

    JKop Guest

    You are right in that any and all constructors are not called yet, but the
    actual memory *is* allocated for the variables at the beginning of the code
    block... I think.
    JKop, May 16, 2004
    #13
  14. John

    Karthik Guest

    John wrote:

    > Hi all:
    >
    > When I run my code, I find that the memory that the code uses keeps
    > increasing.
    > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > memory by the time it finishes execution. But I do not think it needs
    > so much memory. About 500M memory should be enough. I have following
    > questions about memory leak.
    > (1).If in my code I only define constructor for my class, and do not
    > define destructor, will it cause memory leak?
    > (2).If in my code I only use "new" to declare new object, and do not
    > use "delete", will it cause memory leak?
    >
    > For example, in the following code:
    >
    > void class1::function1()
    > {
    > class2 *r1;
    > class2 *r2 = new class2;
    > class2 *rr[20];
    >
    > ......
    >
    > function2(rr);
    >
    > ......
    > //r1 and r2 are also used in function1().
    >
    > }
    >


    As a thumb rule, make sure that the new and delete operators occur
    together in a function body. That makes it easy to maintain and spot the
    bugs.

    That said, where are you free-ing ( deleting ) the memory allocated
    for r2 ( of type class2 ). May be, that might be the culprit.

    --
    Karthik.
    Humans please 'removeme_' for my real email.
    Karthik, May 16, 2004
    #14
  15. John

    John Guest

    "John Harrison" <> wrote in message news:<>...
    > "John" <> wrote in message
    > news:...
    > > Hi all:
    > >
    > > When I run my code, I find that the memory that the code uses keeps
    > > increasing.
    > > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
    > > memory by the time it finishes execution. But I do not think it needs
    > > so much memory. About 500M memory should be enough. I have following
    > > questions about memory leak.
    > > (1).If in my code I only define constructor for my class, and do not
    > > define destructor, will it cause memory leak?

    >
    > Depends on the class.
    >
    > > (2).If in my code I only use "new" to declare new object, and do not
    > > use "delete", will it cause memory leak?

    >
    > Yes.
    >
    > Its a very simple rule, nothing to do with constructors or destructors. When
    > your program runs every new allocates some memory, if you don't do a delete
    > for the same memory then you have a memory leak.
    >
    >
    > >
    > > For example, in the following code:
    > >
    > > void class1::function1()
    > > {
    > > class2 *r1;
    > > class2 *r2 = new class2;
    > > class2 *rr[20];
    > >
    > > ......
    > >
    > > function2(rr);
    > >
    > > ......
    > > //r1 and r2 are also used in function1().
    > >
    > > }
    > >
    > > In the above code, I have two classes and I define constructor for the
    > > two classes and do not define destructor.

    >
    > That's irrelevant.
    >
    > > In function1(), I declare
    > > two pointers of class2 and an array of pointer of class2. The array rr
    > > is used to bring back values from function2(). For r2, I do not use
    > > "delete".
    > > Will r1, r2 and the array rr[] cause memory leak?

    >
    > Where are the deletes? There are no deletes so there are memory leaks all
    > over the place.
    >
    > > The two pointers, r1 and r2, and array rr[] are local variables, when
    > > the code exits function1(), these local variables should be released
    > > automatically.
    > > Am I right?

    >
    > Wrong. The variables destructed and the memory they occupy is 'released',
    > BUT the memory they might be pointing to is not released.
    >
    > void f()
    > {
    > X x;
    > X* xp = new X();
    > ...
    > }
    >
    > When you get to the end of this function, the memory for x and xp are both
    > released. That has nothing to do with the memory pointed to by xp, which is
    > a completely different thing. The memory for a pointer and the memory that
    > it points to are not the same thing.


    >
    > It's very simple, every new must be matched by a delete.
    >


    Does it mean the number of "new" is equal to the number of "delete" in
    a code?
    I think there might be exceptions, for example,

    void f(X* x0)
    {
    X* x1 = new X;
    X* x2 = new X;
    X* x3 = new X;
    ...
    x1 = x0;
    x2 = x0;
    x3 = x0;
    }

    main()
    {
    ....
    f(x0);
    ....
    f1(x0);

    }

    In the above code, x0 exists outside f(). If I "delete" anyone of x1,
    x2 and x3 before the end of f(), x0 will be released. So f1() will not
    run correctly.
    So there is less "delete" than "new".
    Am I right?

    Thanks.

    John (new to C++)
    John, May 17, 2004
    #15
  16. John

    JKop Guest

    John posted:

    > void f(X* x0)
    > {
    > X* x1 = new X;
    > X* x2 = new X;
    > X* x3 = new X;
    > ...
    > x1 = x0;
    > x2 = x0;
    > x3 = x0;
    > }
    >
    > main()
    > {
    > ....
    > f(x0);
    > ....
    > f1(x0);
    >
    > }



    Oh Dear God No!


    First thing, memorize the following:


    Where I use "new", I must use "delete".

    Where I use "new", I must use "delete".

    Where I use "new", I must use "delete".

    Where I use "new", I must use "delete".

    Where I use "new", I must use "delete".

    Where I use "new", I must use "delete".

    Where I use "new", I must use "delete".




    void f(X* pX0)
    {
    X* pX1; //I highly suggest that you prefix the letter
    X* pX2; // p to the front of the names of pointer
    X* pX3; //variables.

    //We have just defined 3 variables of type X*
    //These 3 variables, although being pointer variables,
    //are just like any other variable! They have an
    //address in memory and you can store a value in
    //them. It just so happens that the value you store
    //in them is a memory address!

    //As you can see, I haven't initialized them,
    //and therefore, they can contain any value!

    pX1 = new X; //Here, I have set the value of the
    //variable pX1.

    //"new" is a sort-of function, what it does is
    //allocate memory for an object of size sizeof(X) , and then
    //it calls any constructors applicable to X .
    //It then returns a value of type X* , having
    //allocated the memory. You want to keep note of the
    //address of this memory you've allocated, so you can
    //actually access it in the future - and what better way
    //and place to store it than in a pointer variable!

    pX2 = new X;
    pX3 = new X;

    pX1 = pX0;
    pX2 = pX0;
    pX3 = pX0;

    //You have just set the value of these 3 variables
    //to some other value. One result is that now you
    //no longer know the address of the memory you
    //allocated using "new". Therefore, how the hell
    //are you going to free that memory via a call to
    //"delete"?, as you must specify the address of the
    //previously allocated memory! Doh! And why is this so
    //bad? Because the memory NEVER gets freed, even after
    //you've exited your program. Then, if you run your
    //program again, more memory is "leaked". If you
    //continually do this, it *will* add up, especially on
    //systems that are seldom turned off, eg. internet web
    //servers.
    }

    main()
    {
    ....
    f(&X0); // I assume that you want to pass the address
    // of a variable of type X and of name X0
    ....
    f1(&X0);

    }



    -JKop
    JKop, May 17, 2004
    #16
  17. > >
    > > It's very simple, every new must be matched by a delete.
    > >

    >
    > Does it mean the number of "new" is equal to the number of "delete" in
    > a code?


    No, I mean that at run time every new must be matched by a delete. Often but
    not always that means the news and delete are matched in code, but not
    necessarily.

    > I think there might be exceptions, for example,
    >
    > void f(X* x0)
    > {
    > X* x1 = new X;
    > X* x2 = new X;
    > X* x3 = new X;
    > ...
    > x1 = x0;
    > x2 = x0;
    > x3 = x0;
    > }
    >
    > main()
    > {
    > ....
    > f(x0);
    > ....
    > f1(x0);
    >
    > }
    >
    > In the above code, x0 exists outside f(). If I "delete" anyone of x1,
    > x2 and x3 before the end of f(), x0 will be released. So f1() will not
    > run correctly.
    > So there is less "delete" than "new".
    > Am I right?


    Well I don't think you chosen a good example, because you have three memory
    leaks in f.

    Here's a simpler example

    void delete_me(X* xp)
    {
    delete xp;
    }

    int main()
    {
    X* x1 = new X;
    delete_me(x1);
    X* x2 = new X;
    delete_me(x2);
    }

    In the code there are two news and one delete. But at run time, new is
    called twice and so is delete.

    john
    John Harrison, May 17, 2004
    #17
  18. John wrote:
    >
    > Does it mean the number of "new" is equal to the number of "delete" in
    > a code?
    > I think there might be exceptions, for example,
    >
    > void f(X* x0)
    > {
    > X* x1 = new X;
    > X* x2 = new X;
    > X* x3 = new X;
    > ...
    > x1 = x0;
    > x2 = x0;
    > x3 = x0;
    > }
    >
    > main()
    > {
    > ....
    > f(x0);
    > ....
    > f1(x0);
    >
    > }
    >
    > In the above code, x0 exists outside f(). If I "delete" anyone of x1,
    > x2 and x3 before the end of f(), x0 will be released. So f1() will not
    > run correctly.
    > So there is less "delete" than "new".
    > Am I right?


    When you run this code, the following is happening:

    in main there exists x0

    x0
    +-----------+
    | |
    +-----------+

    then comes the function call to f.
    In f new variables are created and initalized

    X* x1 = new X;
    X* x2 = new X;
    X* x3 = new X;

    That means:



    x0
    +-----------+
    | |
    +-----------+


    f::x1
    +-------+ +-------------+
    | o------------------------------->| |
    +-------+ +-------------+

    f::x2
    +-------+ +-------------+
    | o------------------------------->| |
    +-------+ +-------------+

    f::x3
    +-------+ +-------------+
    | o------------------------------->| |
    +-------+ +-------------+

    And now you do:

    x1 = x0; // x0 beeing the address of variable x0 in main


    x0
    +-----------+
    | |<+
    +-----------+ |
    |
    |
    f::x1 |
    +-------+ | +-------------+
    | o---------+ | |
    +-------+ +-------------+


    You are certainly right, that you must not do a delete on x1, since this
    will try to free the memory occupied by variable x0. But look at the
    lonely rectangle. There is no pointer pointing to it anymore, which
    means you have no way of freeing it again: you created a memory leak.

    The sequence:

    delete x1;
    x1 = x0;

    on the other hand will do:

    This was the starting situation after the allocations

    x0
    +-----------+
    | |
    +-----------+


    f::x1
    +-------+ +-------------+
    | o------------------------------->| |
    +-------+ +-------------+

    delete x1

    x0
    +-----------+
    | |
    +-----------+


    f::x1
    +-------+
    | o------------------------------->
    +-------+

    x1 = x0;

    x0
    +-----------+
    | |<+
    +-----------+ |
    |
    |
    f::x1 |
    +-------+ |
    | o---------+
    +-------+

    and as you can see, there is no longer a rectangle sitting
    in memory with no pointer to it: no memory leak.

    So the correct sequence in your example would be:

    void f(X* x0)
    {
    X* x1 = new X;
    X* x2 = new X;
    X* x3 = new X;
    ...
    delete x1;
    x1 = x0;

    delete x2;
    x2 = x0;

    delete x3;
    x3 = x0;
    }

    And as you can see, at runtime there is exactly one delete
    for every new.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 17, 2004
    #18
  19. John

    Rolf Magnus Guest

    JKop wrote:

    > You are right in that any and all constructors are not called yet, but
    > the actual memory *is* allocated for the variables at the beginning of
    > the code block... I think.


    That depends entirely on the implementation. A conforming program cannot
    find out what the implementation does.
    Rolf Magnus, May 17, 2004
    #19
  20. John

    John Guest

    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.

    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?

    Thanks a lot.

    John


    Karl Heinz Buchegger <> wrote in message news:<>...
    > John wrote:
    > >
    > > Does it mean the number of "new" is equal to the number of "delete" in
    > > a code?
    > > I think there might be exceptions, for example,
    > >
    > > void f(X* x0)
    > > {
    > > X* x1 = new X;
    > > X* x2 = new X;
    > > X* x3 = new X;
    > > ...
    > > x1 = x0;
    > > x2 = x0;
    > > x3 = x0;
    > > }
    > >
    > > main()
    > > {
    > > ....
    > > f(x0);
    > > ....
    > > f1(x0);
    > >
    > > }
    > >
    > > In the above code, x0 exists outside f(). If I "delete" anyone of x1,
    > > x2 and x3 before the end of f(), x0 will be released. So f1() will not
    > > run correctly.
    > > So there is less "delete" than "new".
    > > Am I right?

    >
    > When you run this code, the following is happening:
    >
    > in main there exists x0
    >
    > x0
    > +-----------+
    > | |
    > +-----------+
    >
    > then comes the function call to f.
    > In f new variables are created and initalized
    >
    > X* x1 = new X;
    > X* x2 = new X;
    > X* x3 = new X;
    >
    > That means:
    >
    >
    >
    > x0
    > +-----------+
    > | |
    > +-----------+
    >
    >
    > f::x1
    > +-------+ +-------------+
    > | o------------------------------->| |
    > +-------+ +-------------+
    >
    > f::x2
    > +-------+ +-------------+
    > | o------------------------------->| |
    > +-------+ +-------------+
    >
    > f::x3
    > +-------+ +-------------+
    > | o------------------------------->| |
    > +-------+ +-------------+
    >
    > And now you do:
    >
    > x1 = x0; // x0 beeing the address of variable x0 in main
    >
    >
    > x0
    > +-----------+
    > | |<+
    > +-----------+ |
    > |
    > |
    > f::x1 |
    > +-------+ | +-------------+
    > | o---------+ | |
    > +-------+ +-------------+
    >
    >
    > You are certainly right, that you must not do a delete on x1, since this
    > will try to free the memory occupied by variable x0. But look at the
    > lonely rectangle. There is no pointer pointing to it anymore, which
    > means you have no way of freeing it again: you created a memory leak.
    >
    > The sequence:
    >
    > delete x1;
    > x1 = x0;
    >
    > on the other hand will do:
    >
    > This was the starting situation after the allocations
    >
    > x0
    > +-----------+
    > | |
    > +-----------+
    >
    >
    > f::x1
    > +-------+ +-------------+
    > | o------------------------------->| |
    > +-------+ +-------------+
    >
    > delete x1
    >
    > x0
    > +-----------+
    > | |
    > +-----------+
    >
    >
    > f::x1
    > +-------+
    > | o------------------------------->
    > +-------+
    >
    > x1 = x0;
    >
    > x0
    > +-----------+
    > | |<+
    > +-----------+ |
    > |
    > |
    > f::x1 |
    > +-------+ |
    > | o---------+
    > +-------+
    >
    > and as you can see, there is no longer a rectangle sitting
    > in memory with no pointer to it: no memory leak.
    >
    > So the correct sequence in your example would be:
    >
    > void f(X* x0)
    > {
    > X* x1 = new X;
    > X* x2 = new X;
    > X* x3 = new X;
    > ...
    > delete x1;
    > x1 = x0;
    >
    > delete x2;
    > x2 = x0;
    >
    > delete x3;
    > x3 = x0;
    > }
    >
    > And as you can see, at runtime there is exactly one delete
    > for every new.
    John, May 17, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Y3liZXJzdHJpa2U=?=

    datagrid memory leak?

    =?Utf-8?B?Y3liZXJzdHJpa2U=?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    450
    =?Utf-8?B?Y3liZXJzdHJpa2U=?=
    Jan 3, 2005
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    676
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    337
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    755
  5. Mark Probert
    Replies:
    4
    Views:
    315
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page