Thread: Freeing memory

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    Freeing memory

    I'm not very knowledgable about when memory leaks occure and how to prevent them.

    I have a for(;; ) loop and inside there is a variable, that gets written over each itteration,
    Code:
    char** vecDat = new char *[vehichle.size() + 1];
    At the bottom of the for loop I call
    Code:
    delete [] vecData;
    Should I be doing anything else?

    I was taught to delete[] when new [] had been used, otherwise use delete. But there's new *[] here so I'm not sure.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Can you run your code through a leak checker like Valgrind?

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Consider your question in the context of a `typedef': (Please, do not use such a `typedef' in real code.)

    Code:
    typedef char * type;
    type * s = new type[c];
    delete[] s;
    That isn't different from what you've been told: you've used `new[]' so you need to use a `delete[]'.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    Quote Originally Posted by MutantJohn View Post
    Can you run your code through a leak checker like Valgrind?
    I've never heard of a leack checker. How does it work?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by c_weed View Post
    I've never heard of a leack checker. How does it work?
    Why ask when you can search?

    Valgrind - Wikipedia, the free encyclopedia
    Valgrind

    One of the important skills in a programmers repertoire is searching out information. This was an easy one.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c_weed View Post
    I'm not very knowledgable about when memory leaks occure and how to prevent them.
    Use smart pointers. Use standard library containers. What you've shown can be easy be done with a vector at 0 risk for forgetting to free memory. There really shouldn't be that much raw new and delete in a good codebase.
    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.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Elysia View Post
    Use smart pointers. Use standard library containers. What you've shown can be easy be done with a vector at 0 risk for forgetting to free memory. There really shouldn't be that much raw new and delete in a good codebase.
    I feel like this sidesteps the OP's main concerns.

    @OP

    I think this would help the most : Memory leak - Wikipedia, the free encyclopedia

    And if you're on Linux, Valgrind is indispensable. Windows programmers just write leaky code is all.

    Oh wait, I just googled it and I found this : c - Is there a good Valgrind substitute for Windows? - Stack Overflow

    Valgrind is a lot of things in one and I hope that link can help you find some useful tools. What Elysia said is smart but for the sake of understanding and education, I hope this info can help too.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    I feel like this sidesteps the OP's main concerns.
    I don't see how since the concerns seemed already addressed anyway. Besides, you don't write code like the OP's in today's C++, so the example is outright wrong.
    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.

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I will admit, this example is very confusing to the eye. But prior to my post, I don't think anyone had answered the OP's questions about what a leak is specifically and what causes it. Granted, all I did was link to wiki and then stack overflow so that's about that, I guess.

  10. #10
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    By all means, you should follow Eylsia's advice and use RAII (RAII) to manage your resources. Avoid doing it manually where ever possible. But to get back to your potential leak. If you still suspect that you have a leak, it is not in the code you've shown us. However, since vecData is an array of char pointers, maybe you're also doing something like this in your code:

    Code:
    for ( std::size_t i = 0; i < vehichle.size() + 1; ++i )
    {
        vecDat[ i ] = new char[ 100 ];
    }
    If so, then in addition to delete[]-ing your vecDat array, you obviously must also delete the various "sub-arrays" when you're done with them:

    Code:
    for ( std::size_t i = 0; i < vehichle.size() + 1; ++i )
    {
        delete [] vecDat[ i ];
    }

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by c_weed View Post
    At the bottom of the for loop I call
    Code:
    delete [] vecData;
    Should I be doing anything else?
    You should probably use unique_ptr, which is meant for this case. Example here std::unique_ptr - cppreference.com

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c99tutorial View Post
    You should probably use unique_ptr, which is meant for this case. Example here std::unique_ptr - cppreference.com
    No, in this case, a vector is more suitable, as has been mentioned earlier.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  2. Freeing memory
    By vbdave78 in forum C Programming
    Replies: 7
    Last Post: 11-18-2009, 12:56 PM
  3. Freeing memory
    By C_ntua in forum C Programming
    Replies: 17
    Last Post: 06-29-2008, 04:42 AM
  4. freeing memory
    By bartleby84 in forum C Programming
    Replies: 1
    Last Post: 04-17-2007, 01:29 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM