Thread: Best way to avoid memory leak

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Best way to avoid memory leak

    I have some functions that can cause memory leaks. For example doing something like:
    Code:
    typedef struct
    {
        Uint32 *part;
        Uint16 size;
    }bigint;
    
    bigint add(bigint a, bigint b)
    {
        // Do something
        return result;
    }
    
    int main()
    {
        
        bigint a = make_bigint();  // Memory allocated here
        bigint b = make_bigint();
    
        a = add(a, b);                  // old allocation lost here
    }
    I end up losing the old allocated space in a without freeing it. Any suggestions as to how best to deal with this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know the whole picture, but an assert checking that the pointer is NULL before assigning a new bigint, perhaps? That way you would know if you still have a pointer that isn't released. Unless, of course, you have circular references, in which case I don't think there would be an easy way except to use C++...
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you look at the interface provided by the GMP, you will find that they provide a third parameter to store the result. This approach could be better in that the add function will then have full control over how to assign the result.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One way would be to make the user responsible for allocation of the result as well [and thus, no hidden deallocation or other problems with "numbers getting overwritten".

    Using a 3-argument add function would do the trick:
    Code:
    void add(bigint a, bigint b, bigint c)
    {
        // a = b + c [or c = a + b]. 
    }
    However, you then need to accept calls like this:
    Code:
    bigint x = make_bigint();
    bigint y = make_bigint();
    
    add(x, x, y);
    add(y, x, y);
    add(x, x, x);
    But it avoids the allocation of memory that isn't being freed because the pointer is being "lost" in the operation.

    --
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    If you look at the interface provided by the GMP, you will find that they provide a third parameter to store the result. This approach could be better in that the add function will then have full control over how to assign the result.
    Note: This is the same answer I came up with, but I didn't know that GMP uses that method - although it does of course make sense.

    --
    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.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok, thanks. I'll try get it working passing the result in.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If its any consolation, I just spent the last couple of days on a prototype of my own variable-sized two's complement big-integer number class.
    It's C++ though of course, and it's up on my website already, but I'm still working on it.
    Is there any particular reason to only use C for this?
    Last edited by iMalc; 03-22-2008 at 01:34 PM.
    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"

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Funny you mention it; I was just thinking of converting it to C++. Garbage collection would be nice, and its one instance where operator overloading would make using the code much simpler. Might even be possible to template the chunk size, but thats getting ahead of myself. I had a look at the version on you site before, nicked a couple of ideas, some of just went over the top of my head. I'm still not all that used to C++.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    Funny you mention it; I was just thinking of converting it to C++. Garbage collection would be nice, and its one instance where operator overloading would make using the code much simpler. Might even be possible to template the chunk size, but thats getting ahead of myself. I had a look at the version on you site before, nicked a couple of ideas, some of just went over the top of my head. I'm still not all that used to C++.
    C++ doesn't have garbage collection, under normal circumstances. There are GC add-ons that you can use in C++, with more or less likelihood of sucess.

    But operator overloading is certainly an advantage.

    --
    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.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So are smart pointers.
    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.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Lol, I guess that shows how much I know about C++. Must have been getting it confused with Java or something o_0

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mike_g View Post
    I had a look at the version on you site before, nicked a couple of ideas, some of just went over the top of my head. I'm still not all that used to C++.
    You're welcome. If you'd like me to explain any of it, let me know.
    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"

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    I have some functions that can cause memory leaks. For example doing something like... I end up losing the old allocated space in a without freeing it. Any suggestions as to how best to deal with this?
    The best way to avoid leaking memory is to not allocate any in the first place. Why are you allocating memory inside a "bigint" class?

  14. #14
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    For well known problems it is usually best not to have to deal with their solutions.
    After some searching for my own needs i came up with this attractive open source
    big integer implementation. It's written in ANSI C and there are also wrappers
    that have inline assembler optimizations for x86 CPU's. I tested it and it is fairly fast,
    and straightforward to use. There is no library file provided by the authors, but one
    can be easily constructed.
    It can be found here
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For well known problems it is usually best not to have to deal with their solutions.
    I think the point of this exercise is to learn how to create one's own bigint library, and hopefully learn a thing or two about mathematics and programming from it. I already posted a link to the GMP, so mike_g could have switched to using that yesterday, but would miss out on the exercise by doing so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Memory Leak
    By Berticus in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 05:11 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM