ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
stringsep works like strsep except if the token is not found it returns NULL,but dosn't NULL the entire original string;it just leaves it as it is.
The origin varibale is a complete http header.
This function breaks the header down into header field name and header field value pairs which are stored in a HTTPFIELD structure as type char *.
The problem I have created for myself is making a function to free all this memmory once the calling fuction has finished with the resources.
The origin parameter was created dynamically on the heap.
It is then broken up as previously described in this function.
By breaking it up like that,am I making the memmory unretrievable or
is it just a case of calling free on fields[n]->field_value and fields[n]->field_name ?? and then fields[n]??
These values are just pointers to different parts of the original origin variable allbeit with nulls inserted in between them so I guess not.!?
I guess what I am asking is,if i call free on the memmory address at the start of char *origin will that do the trick,or will the nulls inserted amongst it by stringsep stop it from freeing all the memmory.
Sorry if this is fuzzy.Any questions pls just ask.
You need to free anything that you malloc. If fields[n]->whatever are just pointers into some other memory then you don't free those, just the origin. I would suggest that it's a good idea to have separate create/delete functions for each structure. That keeps memory management contained to a few functions and makes it easier to relate each malloc to it's corresponding free.
In no way the free() implementation can associate that both fields and fields[n] contains pointers that must be freed as well. You must free() each fields[n] manually, possibly _after_ fields[n]->field_name and fields[n]->field_value in the case they were malloc'ed as well. Then you may free() fields.
There's a memory leak in the way you're using realloc(). When realloc() returns NULL, it doesn't free() the original pointer. It does only if it was successful. So if you rewrite the original pointer variable with a NULL, you won't be able to use the original value to either continue using it or free() it. So check return values from malloc(), calloc(), realloc(), etc. To avoid the leak, you may use a wrapper around realloc() that frees the pointer if realloc() failed. It exists on FreeBSD and is called "reallocf()"
You made two serious mistakes:
1. If (p && q) is false, fields[n] is not initialized. This results in wild pointers.
2. You didn't check the return value of malloc/realloc. Your program will segfault if these functions fail silently.
Thnks everyone for the excellent advice,especially primo as I would not have picked up on the realloc issue.
Another naive question.
Or maybe the same question put more simply.!?
if buffer=(char *)malloc(sizeof(char *) * 100);
if
Code:
buffer[2]=buffer[22]=buffer[44]=buffer[99]=NULL;
a call to free(buffer) will still free all 100 bytes right?
Essentially that is all the structures mentioned b4 are.Pointers to areas within one larger memory area on the heap,call it buffer;which are segmented,(No pun intended) into smaller chunks with NULLS.It is these smaller chunks that make up field_name,or field_value.
a call to free(buffer) will still free all 100 bytes right?
Yes. You have 1 malloc, so you only need 1 free. Any other pointers into that memory should not be freed (since what they point to is not an allocated block). Obviously though, once the memory is freed, all those pointers become wild and must not be referenced any further.
If you have a memory leak, then the number of
allocs and the number of frees will differ
(you can't use one free to release the memory
belonging to more than one alloc).
It seems that realloc calls free each time it is invoked,
Not exactly, if there is enough room to extend to block without reallocating, it will do that. If more memory has been malloc'd in the meantime, then there will likely not be enough room, in which case it mallocs fresh memory, copies the old block, and then frees it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.