Thread: malloc/morecore/free questions

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    54

    malloc/morecore/free questions

    I'm reading the chapter on the Unix System Interface from K&R The C Programming Language, and I'm trying to understand how malloc, morecore, and free interact with the free list.

    What happens to the header of a free block when the block is allocated? Does it "dissolve" into the free space returned to the caller of malloc?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    Malloc returns a pointer p
    p - some amount is where malloc "hides" all the information needed for when you call free()
    p + some amount is where you store your data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47

    More question about malloc and free

    hello evrybody,
    I'm new here, I'm from Italy and i learn to program in C some years ago.
    After a long time without programming I'm approaching C another time.
    I have some trouble understanding the right way to use free().
    There is a structure like this:
    Code:
    struct item_t{
       int value;
      char *name;
    }
    and in the main a pointer to this structure like this:
    Code:
    item_t *values;
    if i make
    Code:
    values=(item_t *) malloc (10*sizeof(item_t));
    and in some other function getting as imput *values (or in the same main) I will allocate a name for values[i].name
    do i free evrything just with the command free(values) or not?

    Sorry for my bad english and if I do not write in a good way my trouble, I hope you undestrand what I mean.
    Many thanks
    ZaC

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, you free everything. It is a good question though, not very clear in the beggining.

    As a rule, you free() EXACTLY what you malloc.

    So just lookup whatever malloc you have and put the appropriate free(). You might also want to free() int the opposite order you malloc. That will save you some error in some memory allocation situations.

    Consider the following. You want to malloc memory for each array of characters (string) that *name will point at:
    Code:
    MALLOC:
    values=(item_t *) malloc (10*sizeof(item_t));
    for (int i=0; i < 10; ++i)
       values[i].name = (char *)malloc(100*sizeof(char));
    FREE:
    for (int i=0; i < 10; ++i)
        free(values[i].name);     //opposite order, first the last malloc
    free(values);                      //opposite order, second the first malloc
    
    //Two mallocs, two frees
    You do the opposite order for obvious reasons. If you de-allocate values then values[i].name won't be a valid accessed pointer because values would have already be freed
    Last edited by C_ntua; 06-20-2008 at 06:03 PM.

  5. #5
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Thanks for the fast replay.
    Sorry but I still do not understand if with free(values) I'll free values[i].name too (even if it was "mallocated" separately).
    Last edited by ZaC; 06-20-2008 at 06:12 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by ZaC View Post
    Thanks for the fast replay.
    Sorry but I still do not understand if with free(values) I'll free values[i].name too (even if it was "mallocated" separately).
    Except you don't. free(values) frees ONLY the memory allocated with the malloc call of values -- it does NOT free values[i] for any i.

  7. #7
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Thanks I replayed before C_ntua edited his post with the full explaing

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    EDIT: I was writting more explaining...but, nevermind, i thought you asked for values[i].value
    Last edited by C_ntua; 06-20-2008 at 06:17 PM.

  9. #9
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Yes thanks as i wrote I understand evrything reading the code you put editing
    Thank you very much

  10. #10
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47

    Question another question

    Is possible to set free just portion of values?
    E.G.: i'm tring to load in values n-item_t from a file. For some problem in file reading I loaded only n-5 items. May i free from n-4 to n the array values? How?

    EDIT: perhaps freeing the item in excess is not important.
    Last edited by ZaC; 06-21-2008 at 07:47 AM.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Hmm, good question.
    You use realloc() http://www.cplusplus.com/reference/c...b/realloc.html

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by ZaC View Post
    Is possible to set free just portion of values?
    E.G.: i'm tring to load in values n-item_t from a file. For some problem in file reading I loaded only n-5 items. May i free from n-4 to n the array values? How?

    EDIT: perhaps freeing the item in excess is not important.
    You can free values[n-4], values[n-3], values[n-2], and values[n-1] separately, sure (just modify the for loop to start at n-4). You just have to remember that you did it, so that you don't free them again later.

  13. #13
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    thanks tabstop and C_ntua
    realloc is what i was looking for name fore unloaded items are not already allocated so the for loop is not necessary. I tought that to free unloaded items I should use something like this:
    for(i=n-4;i<=n;i++) free(values[i]);
    realloc do what i want, if i understand well

    PS: always sorry for my english

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM