permaculture playing cards*
The moose likes Linux / UNIX and the fly likes realloc() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "realloc() method" Watch "realloc() method" New topic
Author

realloc() method

Parag Mokal
Greenhorn

Joined: Jan 14, 2001
Posts: 19
Hello Everybody,
I've Red Hat Linix installed on my machine. I've problem with the realloc(void *p, size_t size) method in C. Here's the code
<CODE>
char *p;
p = (char *)malloc(sizeof("computer"));
strcpy(p,"computer");
printf("%s \n",p);
p = (char *)realloc(p,sizeof("computerization"));
printf("%s \n",p);
</CODE>
After running the program, the first printf prints word "computer", but on the second line I get the error
segmentation fault (core dumped)
The realloc method just doesn't work. The same code works at my institute. Can anyone help me with this?
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Originally posted by Parag Mokal:
<CODE>
p = (char *)malloc(sizeof("computer"));
...
p = (char *)realloc(p,sizeof("computerization"));
</CODE>

Compare these two lines, you should see the problem. You try to realloc a smaller memory allocated to become larger, it should report a fragmentation error. Your old machine is simply more tolerant than the new one, the new one is better.
You can free the old one, and alloc a new one instead.

JavaChina has been moved to
http://javachina.developergroup.org/

Parag Mokal
Greenhorn

Joined: Jan 14, 2001
Posts: 19
If I had to free the memory and use it, I would've used the malloc method only. The purpose of the realloc() method is to change the already allocated memory and this code runs fine on the machine in my institute. I've Redhat Linux 7.0 installed. The problem is still not clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: realloc() method
 
Similar Threads
Regex - Java or Perl
C2143 error and I can't find it
problem with catching data with GET
Segmentation fault realloc array from a function
How to use excel file in C program for file handeling?