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.
I am trying to learn c at the moment and I am having a problem freeing a block after I have allocated and used it. I have checked the block is valid using gdb before it hits the crash with segv using gdb and it looks to be intact so I can't understand why I can't free the block.
The code which is causing me trouble is
Code:
char **rules = splitString(&rule_size, rule_string, ",");
if (!rules){ return NULL; }
for (forloop=0; forloop < rule_size; forloop++){
rule = strip(rules[forloop]);
//Do some rule checking.
free(rule);
}
free(rules);
What is the problem actually? Does it give you an error?
Or is it that you don't see memroy usage dropping in tools like 'top'? In the latter case there probably isn't any problem. The kernel will have registered that you free'd the block of memory, and may decide to keep the memory block as a part of your process to re-use it later when your program asks for another block.
There is an obvious error that fits the symptoms. But I didn't check carefully enough to see if it is the only such error nor even if it is the error that would cause the symptoms first.
Quote:
Originally Posted by pgpython
results = (char **) malloc(sizeof(data) + 3);
sizeof(data) does not do what you expect. Passing char data[] does not pass the size of the array, just its address.
Even if you had the size of the char array, I don't think the allocation would be correct. You would then be depending on the average split length being greater than the number of bytes in a pointer.
The actual error generally means you used more of a memory allocation than you allocated, overwriting the information right after that allocation. Clearly you do so with this results allocation. I don't know if you also do so with other allocations.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.