Sign in to follow this  

Freeing allocated memory in C

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there. I am new to the forum, but have been looking at it for a short while now. Seems nice and busy with a good community and since I have newly started teaching myself some C I thought to join in the fun. My first question is about the free() function in C. I have a structure list I need to free up after allocating space for it and I am unsure whether the string needs to be unallocated seperately or whether it'll go when I unallocate the structure entity itself. Here's the structure definition: struct _keywordlistype { char *keyword; int dummy; /*Tells us what to do when we find it atm just a dummy*/ struct _keywordlisttype *next; }; Now when i populate the list I need to allocate for an instance of the structure and i assume also space for the keyword string. So again. How would i free the keyword seperately? Since when i try something like: free(keylist->keyword); I would get an an "dereferencing incomplete type" error; Hope it makes any sense and thanks for your trouble. vk

Share this post


Link to post
Share on other sites
You will have to free the char* seperately.

The variable keyword is just a pointer to a memory location. Unalloc the structure just frees the pointers memory (32bit)...

Can you gice a little more code perhaps? what is keylist?

Share this post


Link to post
Share on other sites
Quote:
Original post by vulkhan
Hi there.
I am new to the forum, but have been looking at it for a short while now. Seems nice and busy with a good community and since I have newly started teaching myself some C I thought to join in the fun.
Welcome aboard!

Quote:
My first question is about the free() function in C. I have a structure list I need to free up after allocating space for it and I am unsure whether the string needs to be unallocated seperately or whether it'll go when I unallocate the structure entity itself.
You need to deallocate it seperately. Consider the relevent member in _keywordlisttype:
char* keyword;

The type of that variable is 'char*,' or 'pointer to a char.' On a 32-bit system, pointers like this one are 32 bits long, or four bytes - and that never changes. So how is it that you're able to 'store' a 100-byte string in a four-byte variable?

The answer is, you actually don't. A 100-byte string needs 100 bytes of memory, somewhere, to store the string in. The four-byte pointer is used to store the address of that block of memory (to 'point' to it).

So, when you free() the structure, you release the block of memory that was used to store the pointer - but the 100-byte string is happily elsewhere, not-freed. It's pretty important that things happen this way - you could have other pointers to the same block of memory elsewhere, for example, and the system usually can't know that. Of course, when you free() the structure, you lose the ability to read the value of the pointer - so you lose the address of your 100-byte block of memory, and thus the ability to free it, so you need to make sure you either (a) free it first, or (b) save the value of the pointer to another location before releasing the structure.

Quote:

Now when i populate the list I need to allocate for an instance of the structure and i assume also space for the keyword string.
Heh, ok, so I'm telling you stuff you already know [smile]

Quote:

So again. How would i free the keyword seperately? Since when i try something like:
free(keylist->keyword);
I would get an an "dereferencing incomplete type" error;
Conceptually, that works - but the compiler's choking on the 'keylist' variable. It knows what type of variable it is - presumably, _keywordlisttype - but it isn't happy with it's knowledge of the _keywordlisttype structure. I can't find a "dereferencing incomplete type" error in MSDN - I guess you're not using MSVC - but my guess is that you've not brought that structure into the current translation unit, through something like an #include.

Share this post


Link to post
Share on other sites
Quote:
Original post by vulkhan
Hi there.
I am new to the forum, but have been looking at it for a short while now. Seems nice and busy with a good community and since I have newly started teaching myself some C I thought to join in the fun.


Hello and welcome. :)

Quote:
Original post by vulkhan
So again. How would i free the keyword seperately? Since when i try something like:
free(keylist->keyword);
I would get an an "dereferencing incomplete type" error;


In the worst case you could bypass the error by something like
char *pTemp = keylist->keyword;
free(pTemp);

It isn't slick but I guess it'll work.

Best of luck
/Staffan

Share this post


Link to post
Share on other sites
Thanks for the responses guys. Much appreciated.
Tbh superpig I didn't know the exact size of pointers, knowing that will simplify allocating pointer arrays some, so thanks for that.

For skanatic I have some code. The problem seems to lie in my understanding on how C like its structures declared.

Here is the full monty(sorta):

struct _keywordlistype
{
char *keyword;
int dummy; /*Tells us what to do when we find it atm just a dummy*/
struct _keywordlisttype *next;
};
typedef struct _keywordlisttype keywordlisttype;

/*top secret useless program code here...*/

void freekeyword(keywordlisttype *listentry)
{
/*Free string seperately before freeing instance of list*/
free(listentry->keyword);
free(listentry);
return;
}

I'd pass the pointer to the current object to freekeyword() and hopefully have my memory back.

The exact error is: Dereferencing pointer to incomplete type.
If I change it to not pass by reference the error is similiar but in the function declaration. Which makes me think I just don't know how to do structures :D

Related to this is that when I try make an instance like so:

keywordlisttype newentry;

I have the error: "Storage size of 'newentry' isn't known"

So yes .. I obviously don't understand the structures well.

vk

o btw its all still in the same file.


[Edited by - vulkhan on January 2, 2005 11:21:40 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by vulkhan
Tbh superpig I didn't know the exact size of pointers, knowing that will simplify allocating pointer arrays some, so thanks for that.
You don't need to know the exact size, and it may vary from platform to platform. That's why C has the sizeof operator:
char **ptrArray = (char **)malloc(1024 * sizeof(char *));


As for the rest of your code, I think the problem lies with your typedef, for some reason. Read this for further information.

Share this post


Link to post
Share on other sites
umm.. I found the problem.
And well ... it's really rather lame :P

The structure is spelt with a single t..
and the typedef I spelt it the way I use it in the rest of the program.
So the two weren't actually related.

Well..I'll go eat my carrots in the corner now.

Thx for the help and sorry for wasting your time guys.
:(

vk

Share this post


Link to post
Share on other sites

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this