Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > strange malloc behaviour

strange malloc behaviour
Thread Tools
WJMoore
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 15, 2002, 09:06 AM
 
I'm writing a program for a subject at uni in C. It involves reading two files in that contain one word per line and perfoming a join on them. My problem is this. In the first part I read both files and store the words in an array. This array of char *'s is dynamically allocated with malloc and resized as necessary using realloc. This first file has over 37000 words in it. If I set the inital size of the array that stores the words to 1000 and increase its size by 100 using reallac each time it gets filled I get the following error using OS X 10.0.4 after about 7 reallocs:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>*** malloc[<font color = blue>5745</font>]: error for object 0x50520: Incorrect check sum for freed object - object was probably modified after beeing freed; break at szone_error
Bus error</font>[/code]

Of note is that none of the elements involved are being modified or freed which makes that error very strange. The same code works without fault on my FreeBSD server and on the Sun server at uni. If I change the initial array size to 4000 and set it to increase it's size by 1000 elements each time the code works fin under OS X as well as the others... Anyone have any suggestions as to why this might be occuring?

TIA
Wesley

[ 05-15-2002: Message edited by: WJMoore ]
     
kamprath
Junior Member
Join Date: Apr 2000
Location: San Francisco, CA
Status: Offline
Reply With Quote
May 15, 2002, 12:12 PM
 
Originally posted by WJMoore:
<STRONG>I'm writing a program for a subject at uni in C. It involves reading two files in that contain one word per line and perfoming a join on them. My problem is this. In the first part I read both files and store the words in an array. This array of char *'s is dynamically allocated with malloc and resized as necessary using realloc. This first file has over 37000 words in it. If I set the inital size of the array that stores the words to 1000 and increase its size by 100 using reallac each time it gets filled I get the following error using OS X 10.0.4 after about 7 reallocs:

&lt;BLOCKQUOTE&gt;&lt;font size="1"face="Geneva, Verdana, Arial"&gt;code:&lt;/font&gt;&lt;HR&gt;&lt;pre&gt;&lt;font size=1 face=courier&gt;*** malloc[&lt;font color = blue&gt;5745&lt;/font&gt;]: error for object 0x50520: Incorrect check sum for freed object - object was probably modified after beeing freed; break at szone_error
Bus error&lt;/font&gt;&lt;/pre&gt;&lt;HR&gt;&lt;/BLOCKQUOTE&gt;

Of note is that none of the elements involved are being modified or freed which makes that error very strange. The same code works without fault on my FreeBSD server and on the Sun server at uni. If I change the initial array size to 4000 and set it to increase it's size by 1000 elements each time the code works fin under OS X as well as the others... Anyone have any suggestions as to why this might be occuring?

TIA
Wesley

[ 05-15-2002: Message edited by: WJMoore ]</STRONG>
Are you allocating memory for each individual char* string in your array of char*'s?
--
Michael F. Kamprath
     
WJMoore  (op)
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 15, 2002, 07:27 PM
 
Originally posted by kamprath:
<STRONG>

Are you allocating memory for each individual char* string in your array of char*'s?</STRONG>
Yep

Wesley
     
Wixar
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
May 16, 2002, 02:37 AM
 
realloc can indeed free memory. How are you using realloc?

Completely broken use of realloc:

void* b = malloc(500);
realloc(b, 1000);

Partially broken use of realloc:

void* b = malloc(500);
b=realloc(b, 1000);

Correct use of realloc:

void* b = malloc(500);
void* temp=realloc(b, 1000);
if (temp) b = temp;
else {
puts("Out of memory!");
exit(EXIT_FAILURE);
}

The thing to keep in mind is that realloc may move your memory block, so you can't keep the old pointer: you have to use the pointer that realloc returns.

Also note that realloc can return NULL, which you must check, as in the third code sample. (It's OK if malloc returns NULL in the third sample because passing a NULL pointer to realloc makes it the same as malloc).

My apologies if you know all this already, but it never hurts to be sure.

-Peter
     
WJMoore  (op)
Grizzled Veteran
Join Date: Jan 2002
Location: Melbourne, Australia
Status: Offline
Reply With Quote
May 16, 2002, 08:57 AM
 
Yeah I am doing all that. The weird thing is the error actually occurs inside the realloc call at some time where it tries to free something (I found this out using gdb). The other thing is realloc doesn't return a NULL pointer, the error occurs before that and causes a Seg Fault or Bus Error (I have seen both - obviously not at the same time though) thus terminating the program.

I might post the relevant code on a webpage and see if anyone notices anything wrong with it.

Wesley
     
   
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:22 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,