Thread: allocating for structures

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    allocating for structures

    I have the following struct..

    Code:
    struct test {
      int id;
      struct test *prev;
      struct test *next;
      unsigned char * type;
    };
    
    struct test *first_test;
    Now when I initialize first_test I allocate memory...

    first_test = malloc(sizeof(test));

    When I free the memory (assume I've assigned values for all the parts of the struct) is the following sufficient?

    free(first_test);

    or do I need to...

    free(first_test->type);
    free(first_test->prev);
    free(first_test->next);
    free(first_test);

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    free(first_test->type);
    free(first_test->prev);
    free(first_test->next);
    free(first_test);
    You will need to do that. But be sure to do the same to the structs inside of your struct though before you free the top-most struct. Make sense? lol

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    well actually when I free one, I'll be sticking the prev and next together to re-link the list.

    But I know what you mean. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating a number of large 3d arrays in C
    By Surrender in forum C Programming
    Replies: 22
    Last Post: 08-19-2008, 08:36 AM
  2. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 04:07 PM
  3. WaitForSingleObject does not allocating...
    By Devil Panther in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2006, 03:49 AM
  4. allocating memory screws up data being read in from file
    By Shadow12345 in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2002, 03:23 PM
  5. Replies: 5
    Last Post: 11-24-2002, 11:33 PM