memory allocation and freeing

This is a discussion on memory allocation and freeing within the Linux Programming forums, part of the Platform Specific Boards category; Greetings, Okay, heres two structures : Code: struct TEST { char *var1; DATA *data; }; struct DATA { char *datavar; ...

  1. #1
    Jase
    Guest

    Question memory allocation and freeing

    Greetings,

    Okay, heres two structures
    :
    Code:
    struct TEST
    {
         char   *var1;
         DATA  *data; 
    };
    
    struct DATA
    {
        char   *datavar;
    };

    lets say I allocate the memory and everything. When I get to the point that I need to free() it I have a questions.

    lets say we have...

    struct TEST test1;

    if I free(test1); does it free *var1, and *data too? or do I need to seperately free them as well? also, if I were to... test1=NULL; will it NULL var1 and data too? or, again, do I need to do those seperatley?

    Thanks, any help on these minor questions will be most appreciated, as well as any other insights.

    -Jase

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you allocate memory for any members explicitly, you need to free it, along with the struct itself.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 04:30 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 04:56 AM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM

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