Thread: When to free up memory?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    When to free up memory?

    I've read that when one uses pointers they must free up the memory to make it available to the system again. However, it seems that I run across a lot of source code that uses pointers and does not free up memory aferwards. When exactly am I suppose to free up memory? By what method am I suppose to free up this memory?

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    If u allocate memory dynamically, that is, by using something like malloc ..then you need to free the memory after you are done with using the dynamically allocated space, so that it is available to the system.If you are talking about memory allocated when you declare a certain variable or pointer, you dont have to free it, because the variables(local ones) will be pushed off the stack once the function returns.

    You can use free() to free the memory.
    Last edited by PING; 09-27-2005 at 11:09 AM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    .
    Join Date
    Nov 2003
    Posts
    307
    I don't know what you've read, but you call free() to "free up" memory, when you are done using the pointer. And, depending on the implmentation, a call to free() does not necessarily give the free()'d memory back to the system, especially in the sense that another process can then use that memory.

    You call malloc() or calloc() or maybe realloc() to allocate memory.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Does the function need to have the "return 0;" statement in it? Does this apply to both C and C++?

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    dynamic memory is allocated from heap.you allocate it using malloc...etc.if you allocate memory dynamically you have to deallocate it yourself otherwise there will be a memory leak or system will not get memory.For larger program it may go out of memory.To deallocate use free(argument is pointer to memory) function provided in standard library.free memory when you are done with it.

    >>Does the function need to have the "return 0;" statement in it?
    Only when you are defining a function you need to specify wat it will return,not when you are using it.free is defined in standard library you are just using it like any other function like printf() etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Example:
    Code:
    #include <stdio.h>
    
    int main(void) {
        char *s, buf[100];
    
        fgets(buf, sizeof(buf), stdin);
    
        s = malloc(strlen(buf)+1);  /* allocate enough memory for buf + NULL */
        strcpy(s, buf);
    
        /* display the two strings to show that they are the same */
        printf("%s%s", s, buf);
    
        free(s);  /* free the allocated memory */
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39
    I always wondered... If someone doesn't free any memory ...after quiting the application does the memory "gets" free automaticaly ...or it just stays "taken"
    Last edited by white; 09-27-2005 at 03:27 PM.
    ----------------

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It depends on the OS. Most systems free allocated memory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. How to Free All Memory ?
    By sergioms in forum C Programming
    Replies: 52
    Last Post: 01-15-2009, 05:02 PM
  4. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  5. free memory in structure
    By franziss in forum C++ Programming
    Replies: 22
    Last Post: 01-08-2007, 05:16 PM