memory leak in the code?

This is a discussion on memory leak in the code? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    memory leak in the code?

    Hello everyone,


    Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a.

    Code:
    try {
        a = new int [N];
        b = new int [M];
    } catch (bad_alloc)
    {
        // if a success, but b fail, should we try to delete[] a here to avoid memory leak?
    
    }

    thanks in advance,
    George

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Yes! All allocated but not freed memory is a leak!
    So yes, you must delete a in the catch, because it's not freed automatically!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You should use smart pointers in the first place.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Unless you set pointers a & b both to NULL to begin with then you wont know which of the two has thrown.
    The answer really depends on the declaration of a & b. Are they locals, or member variables?

    However this is a really useful place to use smart pointers regardless.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi iMalc,


    They are member variables. How to deal with this case?

    Quote Originally Posted by iMalc View Post
    Unless you set pointers a & b both to NULL to begin with then you wont know which of the two has thrown.
    The answer really depends on the declaration of a & b. Are they locals, or member variables?

    However this is a really useful place to use smart pointers regardless.

    regards,
    George

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you are interested in exception safety, you should not be handling more than one naked pointer Guess how much complexity it would add to keep all resources under control, if you were to add a third allocation!

    If you are not interested in exception safety then simply don't bother to catch bad_alloc
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi anon,


    What do you mean "one naked pointer" and "a third allocation"?

    Quote Originally Posted by anon View Post
    If you are interested in exception safety, you should not be handling more than one naked pointer Guess how much complexity it would add to keep all resources under control, if you were to add a third allocation!

    If you are not interested in exception safety then simply don't bother to catch bad_alloc

    regards,
    George

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A "naked" pointer in this context is any "not smart" pointer, so a pointer directly to some data, rather than wrapped in a class.

    A third allocation would be another line, like:
    Code:
       c = new int [X];
    For each new pointer you add, a new line of delete [] would have to be added too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your clarification, Mats!


    If I can not use smart pointer, I will write local try catch block for each naked pointer or define a macro to do that to save typing. :-)

    Quote Originally Posted by matsp View Post
    A "naked" pointer in this context is any "not smart" pointer, so a pointer directly to some data, rather than wrapped in a class.

    A third allocation would be another line, like:
    Code:
       c = new int [X];
    For each new pointer you add, a new line of delete [] would have to be added too.

    --
    Mats

    regards,
    George

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    If there is insufficient memory for the allocation request, by default operator new returns NULL.
    Code:
    try {
        a = new int [M];
        b = new int [N];
    } catch (bad_alloc) {
        if(a != NULL) delete a;
        if(b != NULL) delete b;
        }
    Last edited by abachler; 01-11-2008 at 09:31 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Exactly who/what are you quoting, abachler?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Exactly who/what are you quoting, abachler?
    A non-standard source I would imagine.

    >> If I can not use smart pointer, I will write local try catch block for each naked pointer or define a macro to do that to save typing.
    That would be difficult to do in a macro because you have to delete all previously allocated objects as well.

    You could create a class that holds the pointer, and in the destructor of that class delete the data. That way, if any of the allocations throw an exception, the function will exit and the destructors of the other objects will be called, and the memory will be freed.

    Oh, wait, that's what vector does.

    I have yet to hear a good reason not to use vector instead of a dynamically allocated array. As for non-array dynamic allocations, the same issue applies.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Daved,


    Two more comments,

    1.

    Quote Originally Posted by Daved View Post
    You could create a class that holds the pointer, and in the destructor of that class delete the data. That way, if any of the allocations throw an exception, the function will exit and the destructors of the other objects will be called, and the memory will be freed.
    You should use smart pointer to point to the objects on heap, other than using raw pointers, or else destructor is not ensured to be invoked.


    Quote Originally Posted by Daved View Post
    I have yet to hear a good reason not to use vector instead of a dynamically allocated array. As for non-array dynamic allocations, the same issue applies.
    What is the issue which vector will have, not dynamically allocated array will not have? Could you provide more information or some pseudo code please?


    regards,
    George

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    What is the issue which vector will have, not dynamically allocated array will not have? Could you provide more information or some pseudo code please?
    I think you are misinterpreting Daved due to convoluted language. What Daved meant to say is: "I have yet to hear a good reason to use a dynamically allocated array instead of a vector".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Yes, laserlight.


    I just want to learn in what situations using dynamic allocated array is better than using vector? In my experience, I always think vector is better than dynamic allocated array for a couple of reasons. :-)

    Quote Originally Posted by laserlight View Post
    I think you are misinterpreting Daved due to convoluted language. What Daved meant to say is: "I have yet to hear a good reason to use a dynamically allocated array instead of a vector".

    regards,
    George

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 11-11-2007, 12:27 PM
  2. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  3. Memory leak by simple char
    By Ktulu in forum C++ Programming
    Replies: 42
    Last Post: 11-05-2006, 12:59 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21