freeing mem

This is a discussion on freeing mem within the C Programming forums, part of the General Programming Boards category; i have two functions: Code: MY_TYPE *dostuff() { MY_TYPE *my_type; ... return my_type; } void other() { MY_TYPE *my_type; ... ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    freeing mem

    i have two functions:

    Code:
    MY_TYPE *dostuff()
    {
          MY_TYPE *my_type;
          ...
          return my_type;
    }
    
    void other()
    {
          MY_TYPE *my_type;
          ...
          my_type = dostuff();
          ...
          free(my_type);
    }
    - both pointers point at the same data.
    - only the one free is needed to free all memory.

    is this true?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If I understand you correctly, yes, its true. (I presume you're malloc()'ing memory in the middle of dostuff() somewhere?).

    For each chunk of memory malloc()'d, you must issue one free(). You can store the address of that memory is as many pointers as you like, but you must only free() one of them.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    yes i do mean that.

    its actually a MYSQL_RES type i'm using.

    you don't have to malloc it, but you have to call
    mysql_free_result()!

    thanks!

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    hammer, i'm also having some reallocing problems - it also has to do with freeing mem!

    i had a malloc:

    Code:
    int *sites; //in struct
    
    my_pd->sites = malloc(sizeof(int) * my_pd->max_site_number);
    during testing, i realised i was leaking mem and found that this line was getting called many times in a loop.

    question 1 : will this allocate more and more mem to sites (i.e. leak mem)?

    thinking it would i changed the code to:

    Code:
    realloc(my_pd->sites,sizeof(int) * my_pd->max_site_number);
    
    if(my_pd->sites != NULL){free(my_pd->sites);} //when done
    question 2: is this correct? why do i now get a random seg fault?

    thanks!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's a little difficult to tell without seeing more of the code... but here goes anyway...

    >>leaking mem and found that this line was getting called many times in a loop.
    If you are simply malloc() memory, storing the pointer, then repeating the process, then yes, you are probably throwing away pointers to malloc()'d memory, which is obviously bad.

    >i changed the code to...
    Is realloc() inside a loop? Is the value of max_site_number static? If yes to both, whats the point?

    >realloc(my_pd->sites,....
    When you first call realloc(), what's sites value? If it doesn't point anywhere in particular, it should be set to NULL. And I presume you are trapping the return code from realloc() somewhere?

    If you're wanting dymanic array advice, have a read of this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks hammer, i think i'm going mad,

    am i right in saying then, that realloc should only be called after malloc (or calloc).

    its ok to malloc and free the same variable many times in a loop?

    by the way i'm in oxfordshire!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >am i right in saying then, that realloc should only be called after malloc (or calloc).
    No, not correct! If you pass a NULL pointer to realloc() as its first arg, you simulate malloc(). That way you save yourself the bother of firt malloc()'ing then realloc()'ing.

    >its ok to malloc and free the same variable many times in a loop?
    Yes. Although the question would be why would you want to?

    >by the way i'm in oxfordshire!
    Ahh.. another UK bod
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks hammer,

    i need to get a good c book really. i have more knowledge of oo (c++ & java).

    can you recommend one for my level.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are a few good and a few bad. Do a few searches on here and the web. Here's a good place to start:
    http://www.accu.org/bookreviews/public/
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs and mem allocation
    By reversaflex in forum C Programming
    Replies: 2
    Last Post: 07-03-2008, 08:46 AM
  2. Freeing a char**
    By +Azazel+ in forum C Programming
    Replies: 3
    Last Post: 02-26-2008, 07:27 PM
  3. Replies: 6
    Last Post: 06-02-2006, 09:32 AM
  4. Freeing Linked Lists
    By mrpickle in forum C Programming
    Replies: 4
    Last Post: 01-21-2004, 07:57 PM
  5. freeing list
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-31-2002, 06:45 PM

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