Thread: freeing an array of structures

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    freeing an array of structures

    is that possible

    for eg:

    i have assigned a max of 50 elements in this array of structures

    and i import data from a file and only 15 elements and the remaining 35 are doing nothing

    is it possible to free the memory for that or is there any memory been assigned to be freed?

    i tried this for instance and it didnt work
    Code:
    for (i = counter; i < 50; i++)
    {
    free (data[i]);
    data[i] = NULL'
    }
    and it doesnt work

    any comments
    if u wanna see a working example of this array of structures program u can see it at www.mdofit.com/lab/lab5.c

    it is working fine and is all done but i was hoping to make it more efficient

    thanks

    btw
    anyone is free to use that program for anything they want

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    do a search for linked list's. Using linked lists, you will be able to assign the memory as you use it to a maximum amount, which means you wont waist anything. Makes the program a little tricker to write but if you want efficency with memory then you need to use pointers.

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    No. It is not possible to free an array declared. That's where "Data Structures" play their roles. You can allocate as much memory as you want (ofcourse, depends on the system) and free it the same way.

  4. #4
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    use realloc

  5. #5
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31

    Re: freeing an array of structures

    Originally posted by mackol

    Code:
    for (i = counter; i < 50; i++)
    {
    free (data[i]);
    data[i] = NULL'
    }
    The argument to free() must be a pointer, like free (&data[i]).

    free() is used on memory that has been dynamically allocated. If this is the case of your array then you may use free().

    It is potentially an error to free memory with a pointer to other than the first element of an allocated block.

    Linked Lists would probably provide a better implementation of your program.
    Last edited by char; 06-03-2002 at 11:27 AM.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    71
    i have just started learning about linked lists so i will see what can be done with it

    i did not dynamically assign memory to this array. it was declared like to contain 50 elements from start

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Can't Free(), Realloc() ect unless you Alloc()ed the mem in the first place.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. array of structures help!
    By voodoo3182 in forum C Programming
    Replies: 12
    Last Post: 08-03-2005, 02:58 PM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 03:39 PM