0

I looked for something like this before I posted this post and couldn't find..

I have:

struct foo* fooArray= malloc(sizeof(*fooArray)*5);
struct foo* newFoo = malloc(sizeof(*newFoo));
fooArray[0]=*newFoo;
struct foo* newFoo2 = malloc(sizeof(*newFoo2));
fooArray[1]=*newFoo2;

How do I free each of the elements in fooArray?

free(&fooArray[0]); // Gives me error
free(fooArray[0]); // Doesn't compile
Lee
  • 781
  • 2
  • 11
  • 31
  • Why are you allocating a pointer in `newFoo`? I think you want to allocate a `foo` struct instead, as in `malloc(sizeof(foo))`. – Dacav May 06 '15 at 19:06
  • Almost none of this code actually makes sense – harold May 06 '15 at 19:07
  • Do: `free(fooArray);`. `free()` expects a pointer returned by malloc family functions. Am not sure what your intention is with all those assignments...If that's exactly what you code looks like then you have many more issues... – P.P May 06 '15 at 19:08
  • I am trying to save an array of foo struct and then I want to free each of its elements. The code does save an array of structs. – Lee May 06 '15 at 19:19
  • You can only free what you've allocated. If you allocated the whole array, you must free the whole array. You cannot choose to free things individually unless you allocated them individually. 1 malloc, 1 free, no exceptions. – Lee Daniel Crocker May 06 '15 at 19:28
  • @LeeDanielCrocker So how can I free newFoo, if I only have fooArray? – Lee May 06 '15 at 19:29
  • You did `newFoo = malloc()...`, so `free(newFoo)`. – Lee Daniel Crocker May 06 '15 at 19:31
  • @LeeDanielCrocker I don't have newFoo, I only have fooArray..Is fooArray[0]=NULL enough? – Lee May 06 '15 at 19:34
  • No! You allocated it with `fooArray = malloc()...`, so you have to free it with `free(fooArray)`. You CANNOT free less than you allocated. If you need to free the elements of fooArray idividually, then you must allocate them individually. 1 malloc, 1 free, no exceptions. – Lee Daniel Crocker May 06 '15 at 21:10

2 Answers2

4

First of all this code

struct foo* fooArray= malloc(sizeof(*fooArray)*5);
struct foo* newFoo = malloc(sizeof(*newFoo));
fooArray[0]=*newFoo;
struct foo* newFoo2 = malloc(sizeof(*newFoo2));
fooArray[1]=*newFoo2;

is invalid.

I think you mean the following

struct foo** fooArray= malloc(sizeof(*fooArray)*5);
struct foo* newFoo = malloc(sizeof(newFoo));
fooArray[0]=newFoo;
struct foo* newFoo2 = malloc(sizeof(newFoo2));
fooArray[1]=newFoo2;

To free only these two allocated elements newFoo and new Foo2 you can write for example

free( fooArray[0] );
free( fooArray[1] );

fooArray[0] = NULL;
fooArray[1] = NULL;

If you want also to free the entire array then you can write

free( fooArray[0] );
free( fooArray[1] );

free( fooArray );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Actually it does work for me as I wrote..it saves an array of foo's without ** – Lee May 06 '15 at 19:17
  • @Lee As I wrote your code is invalid. You should decide whether you want to create an array of 5 pointers to objects of the structure or an array of 5 objects of the structure. – Vlad from Moscow May 06 '15 at 19:20
1
struct foo* fooArray= malloc(sizeof(*fooArray)*5);

when you allocate fooArray, you allocate ONE buffer of size 5 times a struct foo. If you want to hold 5 struct foo's, you are good, but you don't have to malloc the array members:

struct foo* newFoo = fooArray
struct foo* newFoo2 = fooArray + 1;
struct foo* newFoo3 = &fooArray[2]; /* array notation */

..and you can only free them all at once

free(fooArray);

BUT...

If you want a set of 5 pointers The size is wrong, and the data type on the left is wrong.

struct foo* fooArray= malloc(sizeof(*fooArray)*5);

should read:

struct foo** fooArray = malloc(sizeof(struct foo*) * 5);

It is a pointer to 5 pointers. Then your array notation can work for allocation and freeing.

fooArray[0] = malloc(sizeof struct foo);
free(fooArray[0]);

BTW, if you want an "array" to grow and shrink dynamically, you don't wan't an array. you want a Single linked list

Just include a (struct foo*) pointer in struct foo and you can do away with fooArray.

Community
  • 1
  • 1
Joshua Clayton
  • 1,669
  • 18
  • 29