Thread: Malloc and Free.....

  1. #1
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36

    Malloc and Free.....

    Hi Guys, got a question here about malloc and free calls...

    Say I want to allocate a dynamic array of struct, I go something like

    tydef struct somestruct mystruct;
    mystruct *pt = (mystruct*)malloc(num*sizeof(mystruct));


    So I can do pointer arthmetic right? For example:

    mystruct *temp_pt = pt;
    for(i = 0; i < num; i++)
    // do some work with temp_pt

    Now, my question is....if I want to free up the allocated memory, do I have to do it individually:

    mystruct *temp_pt = pt;
    for(i = 0; i < num; i++)
    {
    free(temp_pt);
    temp_pt++;
    }

    or: I only have to free from the base of the allocated memory:

    free(pt);



    Thanks in advance!

  2. #2
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    You only have to free from the base of the allocated memory:

    free(pt);

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    44

    Re: Malloc and Free.....

    Originally posted by heljy
    Say I want to allocate a dynamic array of struct, I go something like

    tydef struct somestruct mystruct;
    mystruct *pt = (mystruct*)malloc(num*sizeof(mystruct));
    Just something to note: in C the cast isn't necessary. An assignment to/from a void pointer to a pointer of another type doesn't require a cast. This excludes function pointers IIRC.

    Casting malloc (and realloc etc) can hide a bug... that of not including stdlib.h. On many platforms you 'get away' with it at link time, but as far as C is concerned it's undefined behaviour. It could do anything from toast your hard drive through to making demons (or daemons) fly out of your nose.

    So I can do pointer arthmetic right? For example:

    mystruct *temp_pt = pt;
    for(i = 0; i < num; i++)
    // do some work with temp_pt
    Yep, but ITYM

    temp_pt+i (or *(temp_pt+i) which is equivalent to temp_pt[i])

    or

    temp_pt++ at some point in the loop

    and so on and so forth.

    Now, my question is....if I want to free up the allocated memory, do I have to do it individually:

    mystruct *temp_pt = pt;
    for(i = 0; i < num; i++)
    {
    free(temp_pt);
    temp_pt++;
    }
    Nope!

    or: I only have to free from the base of the allocated memory:

    free(pt);
    Yep! The only valid things you can pass to free are the values which have been returned from malloc, realloc and calloc. Passing anything else (except NULL) is a bug, and a nasty one at that. Usually programs will carry on merilly after this, and then crash spontaneously next time... or the 5000th time you allocate or free some more memory. Again, it's undefined behaviour.

    Ian Woods

  4. #4
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Thanks alot PutoAmo and Hairyian!!

    Hairyian, thats a very detail breakdown of it!! =))

    Now that casting comment is very informative! I did not know that =P

    Thanks once again!
    If only life is as easy as C...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ah, nasal demons. The telltale sign of a CLC goer

    >Now that casting comment is very informative! I did not know that =P
    I find that hard to believe, what with Salem, quzah, myself, and a few others explaining it almost weekly.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Thats because I am not very active here I guess =P

    Guess I should be!! *Motivated now *

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc calloc and free
    By -EquinoX- in forum C Programming
    Replies: 27
    Last Post: 03-26-2009, 10:59 AM
  2. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  5. Ask about free funtion using with malloc
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 04:43 PM