Free Structs

Member
Posts: 64
Joined: 2005.06
Post: #1
I'm a little rusty on my C (read: I never actually learned C by reading a book or other traditional methods, I learned it like I learned Obj-C, BASIC and a number of other languages; I looked something up as I needed to know it), so I'm curious about releasing a struct.

If I had:
Code:
typedef struct DFTexture
{
    float textureOpacity;
    NSString *textureName;
    void *textureData;
} DFTexture;

DFColor *texture = calloc(1, sizeof(DFColor));
// Do stuff that filled struct objects with data
free(texture);

Would free(texture) free not just the pointer to texture but also all the data contained within it, or do I need to explicitly free all the data in it, then free texture?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
free doesn't know anything about the format of the memory block you pass it. If your struct contains allocated memory, you'll need to release/free each field (in this case, textureName and textureData) before freeing the allocated struct itself.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #3
ThemsAllTook Wrote:free doesn't know anything about the format of the memory block you pass it. If your struct contains allocated memory, you'll need to release/free each field (in this case, textureName and textureData) before freeing the allocated struct itself.

For what it's worth, this is one of the strengths of acutal OO languages like ObjC and C++ where you can simply define a proper destructor method.

But, anyway, when I'm dealing with C structs which require complex allocating and free-ing scenarios, I usually define a constructor and destructor function. Sort of like:

Code:
struct DFTexture DFTexture_new( ... )
{
    // malloc your struct, then create your resources...
}

void DFTexture_destroy( struct DFTexture *t )
{
     // free each resource as needed
     // then free(t)
}

This makes it a lot easier to manage.
Quote this message in a reply
Member
Posts: 64
Joined: 2005.06
Post: #4
Yep, I've already created both constructor and destructor functions, the constructor passing a BOOL as to wether it succeeded in creating the new texture, and returns the texture ID using Bitwise &.

I'd love to keep track of all these things using actual names, but it actually adds horribly to the complication, so I've resorted to just keeping track of things by ID, and having accessor functions for creating/destroying/looking up by name, which all it really does is run through the array and finds the ID of the struct containing the name given, then doing the appropriate action using that ID.
Quote this message in a reply
Jones
Unregistered
 
Post: #5
Simple question:

What advantages does managing memory yourself give you? I would think the language is capable of doing so in a competent way. At least when it comes to integers and character templates. I'd guess you'd need that sort of stuff for loading up data files and images, but using it to hold a value like the players health, would that really give me more "control"?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Drawing an OBJ from structs mikey 51 17,119 Jul 13, 2009 09:23 AM
Last Post: mikey
  Weird behaviour with structs and the = operand Jones 2 2,655 Oct 12, 2006 07:11 PM
Last Post: Jones