tags:

views:

409

answers:

8

I have a struct that only contains pointers to memory that I've allocated. Is there a way to recursively free each element that is a pointer rather than calling free on each one?

For example, let's say I have this layout:

typedef struct { ... } vertex;
typedef struct { ... } normal;
typedef struct { ... } texture_coord;

typedef struct
{
    vertex* vertices;
    normal* normals;
    texture_coord* uv_coords;
    int* quads;
    int* triangles;
} model;

And in my code I malloc each of the structs to create a model:

model* mdl = malloc (...);
mdl->vertices = malloc (...);
mdl->normals = malloc (...);
mdl->uv_coords = malloc (...);
mdl->quads = malloc (...);
mdl->triangles = malloc (...);

It's straightforward to free each pointer as so:

free (mdl->vertices);
free (mdl->normals);
free (mdl->uv_coords);
free (mdl->quads);
free (mdl->triangles);
free (mdl);

Is there a way that I can recursively iterate through the pointers in mdl rather than calling free on each element?

(In practice it's barely any work to just write free() for each one, but it would reduce code duplication and be useful to learn from)

+16  A: 

Not really - although you can write a method to do all six frees so that you never miss one.

void freeModel( model* md1 ) {
    free (mdl->vertices);
    free (mdl->normals);
    free (mdl->uv_coords);
    free (mdl->quads);
    free (mdl->triangles);
    free (mdl);
}
Nathaniel Flath
+2  A: 

Throw all of the free's in a function?

John at CashCommons
A: 

I don't believe that's possible in any form of C.

You could code a separate function for that specific structure where you would pass the pointers and free them there.

EDIT: Ups, too late, never saw those answers...

Nazgulled
A: 

Not with those structures. You could add another entry to your top-level 'model' struct containing a list of pointers to be freed, and iterate that list. But I doubt if the added complexity and reduced understandability of that solution would be worth it. (Unless you've got a lot bigger and more deeply-nested set of entries to free in the top-level 'model' struct than you are showing here.)

Bruce
+7  A: 

There is no way in the C language to do this, nor would it be desirable - C doesn't know that each member is a distinct pointer allocated via malloc and C contains no run-time type info support to do this - at runtime the compiled code to access the struct is just using offsets off of a base pointer for each member access.

The simplest approach would be to write a "FreeModel" function:

void FreeModel(model* mdl)
{
   free(mdl->vertices);
   ... // Other frees
   free(mdl);
}
Michael
+3  A: 

You could calculate the size needed for all of them together and do one big malloc

sizeof(model)+sizeof(vertex)*nVertices... etc.

assign the result to mdl, result+sizeof(model) to model->vertices...

Then to free it is just one free.

You may have to worry about alignment issues (depending on your platform) but that shouldn't be too tough to figure out. The other problem is that it is a much larger chunk which may present a problem if in a memory constrained environment.

Dolphin
+3  A: 

Such functionality is not built in to C, but you can cheat a little bit by abusing the macro preprocessor:

#define XX_MODEL_POINTERS do { \
  xx(vertices); xx(normals); xx(uv_coords); xx(quads); xx(triangles); \
} while(0)

To allocate:

model *mdl = malloc(sizeof(*mdl));
assert(mdl);
#define xx(N) mdl->N = malloc(sizeof(*mdl->N)); assert(mdl->N)
XX_MODEL_POINTERS;
#undef xx

To free:

assert(mdl);
#define xx(N) free(mdl->N); mdl->NULL
XX_MODEL_POINTERS;
#undef xx
free(mdl);
mdl = NULL;

The nasty bit is that the definition of struct model and the definition of XX_MODEL_POINTERS can become mutually inconsistent, and there's no way to catch it. For this reason it's often better to generate the definition of XX_MODEL_POINTERS by parsing a .h file somewhere.

Metaprogramming in C is never easy.

Norman Ramsey
Ah clever, thanks! Although this solution does look dirty, it avoids code duplication without a performance penalty and it's an approach I haven't seen before. I probably wouldn't use it in practice but it addresses my question with an interesting implementation rather than just re-organizing code.
Kai
Couldn't you make the operation to perform an argument to the macro, rather than having to re- `#define` it each time?
R..
+1  A: 

Take a look at talloc http://talloc.samba.org/ if you do:

model* mdl = talloc (NULL, ...);
mdl->vertices = talloc (mdl, ...);
mdl->normals = talloc (mdl, ...);
mdl->uv_coords = talloc (mdl, ...);
mdl->quads = talloc (mdl, ...);
mdl->triangles = talloc (mdl, ...);

you can then:

talloc_free(mdl);

and talloc will take care of free'ing all the other blocks you called talloc with mdl as the first argument at allocation time (and it will do this recursively you can do talloc(mdl->vertices, ...) and talloc_free(mdl); will get that too)

as an aside there is a slight overhead to using talloc because it needs to track what stuff recurse over, but it's not very much.

Spudd86