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 somewhat new to C, although I have done a bit of programming in C++. My problem is this: I am attempting to double the size of a char * array using realloc() wrapped in the xrealloc() suggested in Libc's info page. However, after calling realloc() with all the right parameters (I've stepped through it with gdb), the block hasn't changed size -- that is, if the initial size was 2 elements, trying to access the 4th element segfaults. I'm still not sure why this happened, but I fixed it by memsetting the new half of the block (elements 2 + 3 in the previous example) to 0. After doing that in the xrealloc() wrapper, I can access the new memory just fine.
I Googled and searched various forums, and haven't been able to find an explanation for this, although I'm sure it's a basic element of the language that I missed somewhere in my reading. If anyone can take a few lines to enlighten me, that would be great.
where num_mutations is the size of retn (initially 2).
retn is modified in a recursive function, it is initially allocated in the code for the base case, and resized by the recursive case. If you need more info, just ask.
In what context do you call memset? Are you saying that the original part of the array (the part that existed before xrealloc) is garbage unless you use memset? The added portion will always be garbage. Please post a brief main function that reproduces the problem (on your end, anyway.) Also, why does your call to xrealloc have 3 arguments?
ta0kira
Hmm, that's weird: I was going through my code again to respond to some of your questions, and it worked. Seeing it with fresh eyes, I suspect that the problem I may have been having was that I was calling xrealloc on the new elements of retn without setting them to NULL or a valid pointer first, so it was trying to increase the size of a block that it didn't have access to. Thanks for asking questions, it seems to have brought some insight!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.