LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2008, 07:26 PM   #1
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
realloc doesn't work unless memset is called


Hi

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.

Thanks
 
Old 02-28-2008, 07:38 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Was the original array created on the heap?
Can you show some code to demonstrate this please.
 
Old 02-28-2008, 08:07 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You're using the return value of xrealloc and not the parameter you passed to it, correct?
ta0kira
 
Old 02-29-2008, 01:01 PM   #4
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
xmalloc():

Code:
void *
xmalloc (size_t size) {
    register void *value = malloc (size);
    if (value == 0)
        abort(); /*fatal ("virtual memory exhausted");*/
    return value;
}
xrealloc():

Code:
void *
xrealloc (void *ptr, size_t size) {
    register void *value = realloc(ptr, size);
    if (value == 0)
        abort();
    return value;
}
Code that creates the array:

Code:
retn = (char **)xmalloc(sizeof(char *) * 2);
Code that resizes array:

Code:
retn = (char **)xrealloc(retn, num_mutations * sizeof(char *), 2 * num_mutations * sizeof(char *));
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.
 
Old 02-29-2008, 03:47 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 02-29-2008, 08:28 PM   #6
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Won't get realloc to work - glibc: invalid old size ruh31 Programming 5 06-08-2011 05:39 PM
using memset() functions for graphics gvp87 Programming 2 02-05-2008 05:10 PM
graphic function with memset() gvp87 Programming 1 01-31-2008 12:04 AM
wget does not work when called by cron kapster Linux - Newbie 2 09-27-2007 03:06 AM
memset vs char arr initialization syseeker Programming 4 03-29-2006 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration