Skip to main content
gamedev.net
🔒 Locked

malloc, free and structs

Started by NoNseNse Dec 5, 2000 at 4:51 PM 2 replies 700+ views
Original Post
NoNseNse
NoNseNse
I''m using the following dtructure: struct s_bone { s_key *Key; inr KeyNum; }; struct s_sam { int NumBones; s_bone *bone; }; of course there are other elements in the structs, removed to save space. I then allocate some memory like this: Sam.bone=(s_bone*)malloc(sizeof(s_bone) * Sam.NumBones); after that I allocate memory for the keys in every single bone: for(i=0; i.Key = (s_key*)malloc(sizeof(s_key) * Sam.bone[i].KeyNum); everything OK so far, works great (I hope there are no errors so far). But now, at the end of the program I want to free the memory. Now the question: is it sufficient to free the Sam.bone, or will I have to free every key first, and then, afterwards the bone? I tried the latter, first freeing the keys, and the bones afterwards, but I get an Runtime Error here. No error if I just free the Sam.bone. But If I just free them, is there a memory leak? THX,, NoNseNse
Cessen
Cessen
I suppose it depends on the compiler. I know that in the ANSI C standard, you have to free everything seperately, otherwise the piece of memory allocated to the sub-variables will not be freed.

However, that is not your problem. Your problem is that you are trying to allocate an array with the malloc function. You can't do that! What your code right now does is allocate new memory, and redirect the *bone pointer to that new piece of memory, leaving the piece of memory it redirected from without even a pointer to it. But all of those memory pieces are still allocated, they simply don't have pointers pointing to them. Basicly, you are making a huge-ass memory leak

Of course, you will want to know how to do it correctly To allocate an array of space, you must use the calloc function. Calloc is simply the array version of malloc.
The syntax of calloc is as follows:
calloc(nmemb, nsize);
"nmemb" is the number of elements to allocate, and "nsize" is the size of a single element. It will then allocate "nmemb" elements, each of which are "nsize" bytes large.
Calloc will return a pointer to the entire array.

An example usage would be:

typedef struct test_struct {
int *array_pntr;
};

test_struct *test;

test = malloc(sizeof(test_struct));

test.array_pntr = calloc(32, sizeof(int));

That will allocate 32 integers inside the structure "test".

Hope this helps!

----------------

I modified it because I forgot the semi-colon after the structure definition. I just love getting syntax errors from the compiler

Edited by - Cessen on December 5, 2000 6:32:09 PM
Heraldin
Heraldin
You don''t have to use calloc to allocate arrays, its syntax is just clearer than malloc.

Make sure you free the same number of blocks as you have allocated. Certainly, it is better practise to free everything you allocate. Windows (and almost all other operating systems) will tidy up after you, but its best not to rely on this. If you''re getting a runtime error, then your data may be corrupted somehow. If you overwrite an allocated buffer, then the heap management can get almighty confused.

I''m a little confused by the KeyNum member of s_bone. Is it the number of keys? Ensure that you never access s_bone::Key[key] for key >= KeyNum. Are you referring to the last key as s_bone::Key[KeyNum]? (it should be KeyNum-1).

The trouble with this sort of bug is that any command could be writing to the wrong bit of memory. Good luck!

Dave
NoNseNse
NoNseNse
THX both of you, I rewrite it all to calloc (simply for the sake of readability, because the syntax is much clearer) and skipped the freeing of the seperate keys. Dave: I used a for(n=0; n.KeyNum; n++) - loop, so that wasn''t the problem (by debugging I found out that the error occured at n=0). It works now, And even after many executions my system is still stable, so I believe no memory leak here. THX again!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.