Thread: memory allocation and freeing

  1. #1

    Question memory allocation and freeing


    Okay, heres two structures
    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.


  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada
    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, 05: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, 05:56 AM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM