free() and structures

This is a discussion on free() and structures within the C Programming forums, part of the General Programming Boards category; Hello, I've searched the internet and these fora for help on this topic, without success: Suppose I have a structure, ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    free() and structures

    Hello,

    I've searched the internet and these fora for help on this topic, without success: Suppose I have a structure, as follows:

    Code:
    typedef struct{
    
    double *vector; int beta;
    }wrapper;
    I initialize a pointer to an instance of wrapper, as follows:

    Code:
    wrapper * initWrapper ()
    {
    
    thisWrapper = (wrapper*)malloc(sizeof(wrapper)); thisWrapper->vector = (double*)malloc(10*sizeof(double)); thisWrapper->beta = 0; return(thisWrapper);
    } int main() {
    wrapper *myWrapper = initWrapper(); int returnFlag = 0; if (wrapper !=NULL)
    returnFlag = 0;
    else
    returnFlag = -1;
    return(returnFlag);
    }
    Now, if I add this line towards the end of main()
    Code:
    free (myWrapper);
    does the pointer myWrapper->vector also get freed, or do I have to write a function to go in and free it?

    Thanks,
    Dev

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    does the pointer myWrapper->vector also get freed
    No.

    You have to specifically free() every allocation you do (e.g. every malloc() should have a corresponding free()). Make sure you free the vector before freeing the wrapper. Once you free the wrapper you can no longer legally dereference that pointer to get to your vector.

    When you realloc() your vector later you still only need to call free() for the vector once of course.

    Also, you don't need to cast the return value of malloc(). That's a C++-ism and is discouraged on this C forum.
    Last edited by itsme86; 07-12-2006 at 03:07 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    Thanks, for both the answer and the suggesion!

    Regards,
    Dev

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Also, you don't need to cast the return value of malloc(). That's a C++-ism and is discouraged on this C forum.
    More info here: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    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. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 2
    Last Post: 01-26-2003, 03:37 AM
  3. freeing an array of structures
    By mackol in forum C Programming
    Replies: 6
    Last Post: 06-03-2002, 11:43 PM
  4. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 11:53 AM
  5. structures and free()
    By sballew in forum C Programming
    Replies: 6
    Last Post: 11-05-2001, 12:23 PM

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