free( ) failing?????? HELP!!!!
I made a 2D array 100,000 by 60 to hold a list of strings. I allocate them with malloc( ) and they work great. I swap parts of large lists into this memory and just keep over writing the memory until I''m done, then free the memory. I''ve been using this for a month now, but never on string lists of more than 1 million. I tried to do a list of 1.1 and 2.3 mil a few days ago and it crashed. "No big deal, I''ll just debug and find out what happened..." It was crashing after freeing a few thousand of the array....
//...do stuff...
for(i=0;i<100000;i++)
{
free(array); //crashes here when i is about
//8190 - 8250 on a list of 2.3 mil
}
free(array);
It''s never consistant, only in the fact that it only gets to about 4000 when I use a list of 1 million. I have read the free( ) details, and there should be no problem as far as I know. Does anyone know why this is happening? I have never seen this before, and it is now an important part of this program, so a rewrite would suck...but then again, what is to say this won''t happen again if I rewrite the function...what do I do?
"Victims...aren't we all?" -Brandon Lee, the Crow
1) The code you posted won''t work - it calls free() with the ***same*** pointer (array) 100000 times. It should surely be:
&(array) - depending on what the data type is defined as.
2) Do you check that each allocation (malloc) has succeeded ? or do you just assume it''ll work ?...
3) An array of 2.3 million ?!! - thats seriously over-comitting memory - and a sign you need to re-evaluate your algorithm:
2300000 * 60 = 138000000 = 131Mb
Thats ignoring the fact that with a debug version of the runtime library running theres going to be an extra overhead per-allocation of at least around 32bytes for debug heap stuff (2*nomansland, heap block, list node).
The above would fail on allocation if you ran out of harddisk space (swap file can''t grow any more).
Could you post some more proper details:
A) The exact allocation code and data type, eg: is the code doing something like:
char** array;
array = malloc( somehighnumber );
for (i=0; i{
array = malloc( 60 );<br>}<br><br><br>B) What you mean by "crashed" - a "Damage" error ?, an access violation ?, blue screen ?<br><br><br>C) What is the exact free code (what you posted would give a Damage error or similar with the debug heap). </i> <br><br>–<br>Simon O''''Connor<br>Creative Asylum Ltd<br><A HREF="http://www.creative-asylum.com">www.creative-asylum.com</A>
&(array) - depending on what the data type is defined as.
2) Do you check that each allocation (malloc) has succeeded ? or do you just assume it''ll work ?...
3) An array of 2.3 million ?!! - thats seriously over-comitting memory - and a sign you need to re-evaluate your algorithm:
2300000 * 60 = 138000000 = 131Mb
Thats ignoring the fact that with a debug version of the runtime library running theres going to be an extra overhead per-allocation of at least around 32bytes for debug heap stuff (2*nomansland, heap block, list node).
The above would fail on allocation if you ran out of harddisk space (swap file can''t grow any more).
Could you post some more proper details:
A) The exact allocation code and data type, eg: is the code doing something like:
char** array;
array = malloc( somehighnumber );
for (i=0; i
array = malloc( 60 );<br>}<br><br><br>B) What you mean by "crashed" - a "Damage" error ?, an access violation ?, blue screen ?<br><br><br>C) What is the exact free code (what you posted would give a Damage error or similar with the debug heap). </i> <br><br>–<br>Simon O''''Connor<br>Creative Asylum Ltd<br><A HREF="http://www.creative-asylum.com">www.creative-asylum.com</A>
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
Sorry, I forgot a little bit in my posted code. Well, here is the whole caboodle.
/*Note: The array is 100,000 elements of 60 characters, and those elements are filled with strings from a file of x strings 100,000 at a time and processed until x have been loaded and processed. The array is never bigger than 6 megs this way.*/
Alloc code:
FileFrame=(char**)malloc(sizeof(char*)*100000);
if(FileFrame!=NULL)
{
for(i=0;i<100000;i++)
{
FileFrame=(char*)malloc(60);
if(FileFrame==NULL)<br> {<br> return(-1);<br> }<br> }<br>}<br>else<br>{<br> return(-1);<br>}<br><br><br><br>DeAlloc code:<br><br>for(i=0;i<100001;i++)<br>{<br> free(FileFrame); //dies here; access violation <br>}<br>free(FileFrame);<br><br><br>This is right out of the code, though I forget what the actual error was. I think it was an access violation. The odd thing to me is that it works on smaller files of less than a million strings, but fails on free( ) on more than a million…i don''t see the correlation. Does this code help?<br> </i>
/*Note: The array is 100,000 elements of 60 characters, and those elements are filled with strings from a file of x strings 100,000 at a time and processed until x have been loaded and processed. The array is never bigger than 6 megs this way.*/
Alloc code:
FileFrame=(char**)malloc(sizeof(char*)*100000);
if(FileFrame!=NULL)
{
for(i=0;i<100000;i++)
{
FileFrame=(char*)malloc(60);
if(FileFrame==NULL)<br> {<br> return(-1);<br> }<br> }<br>}<br>else<br>{<br> return(-1);<br>}<br><br><br><br>DeAlloc code:<br><br>for(i=0;i<100001;i++)<br>{<br> free(FileFrame); //dies here; access violation <br>}<br>free(FileFrame);<br><br><br>This is right out of the code, though I forget what the actual error was. I think it was an access violation. The odd thing to me is that it works on smaller files of less than a million strings, but fails on free( ) on more than a million…i don''t see the correlation. Does this code help?<br> </i>
"Victims...aren't we all?" -Brandon Lee, the Crow
June 04, 2001 02:22 PM
Your alloc code is wrong...
FileFrame=(char**)malloc(sizeof(char*)*100000);
if(FileFrame!=NULL)
{
for(i=0;i<100000;i++)
{
/*
try changing this line from
FileFrame=(char*)malloc(60);
to
*/
FileFrame=(char*)malloc(60);
/*
otherwise you just blow away your original
malloc of char* * 100000
/*
if(FileFrame==NULL)<br>{<br>return(-1);<br>}<br>}<br>}<br>else<br>{<br>return(-1);<br>} </i>
FileFrame=(char**)malloc(sizeof(char*)*100000);
if(FileFrame!=NULL)
{
for(i=0;i<100000;i++)
{
/*
try changing this line from
FileFrame=(char*)malloc(60);
to
*/
FileFrame=(char*)malloc(60);
/*
otherwise you just blow away your original
malloc of char* * 100000
/*
if(FileFrame==NULL)<br>{<br>return(-1);<br>}<br>}<br>}<br>else<br>{<br>return(-1);<br>} </i>
June 04, 2001 02:26 PM
woah, something messed up in my post, maybe yours too.
anyways, what i was trying to say above was that
your malloc of the individual elements was missing ''i''
and just blowing away the original malloc. however,
since it just does not show up in the post, maybe
im wrong.
anyways, what i was trying to say above was that
your malloc of the individual elements was missing ''i''
and just blowing away the original malloc. however,
since it just does not show up in the post, maybe
im wrong.
![](tongue.gif)
No, the memory is alloced to each i element, it just gets erased in the post. The code works. It even works to a point. I''m trying to figure out why it stops working at free( )? Any new ideas?
"Victims...aren't we all?" -Brandon Lee, the Crow
June 04, 2001 02:35 PM
Perhaps where you put data in each element you
dont put it in correctly, and the alloc and
free code works fine.
you could always put
if (fileframe)
free(fileframe);<br><br>to make sure something is in it before freeing..<br> </i>
dont put it in correctly, and the alloc and
free code works fine.
you could always put
if (fileframe)
free(fileframe);<br><br>to make sure something is in it before freeing..<br> </i>
The file is read into a buffer of 6 megs, and then that buffer is parsed character by character into the array, and I have verified the integrity of each of the strings through debug mode, so I know my fill algorythm is working. free( ) returns safely if you give it a NULL pointer, so that is not the problem, and each element is checked at allocation for NULL. I have checked my working code for an accidental reallocation of the pointer, but have not found one. I even used a search for "FileFrame[]=" but have not found it except for the allocation point. I''ll kiss the arse of the person that solves this mystery...I''m lost.
"Victims...aren't we all?" -Brandon Lee, the Crow
June 04, 2001 03:34 PM
im bored and on vacation, send the code to ihatetheinternet@hotmail.com and ill have a look if you''d like..
I dont use C functions like malloc or free but doing memory allocation in C++ is pretty straght forward so here is a function that i use to allocate 2d arrays.
//allocate a two dimentional array on the heap
template < typename T >
void allocate2dArray(T**& array, int numOfRows, int numOfColumns)
{
array = new T*[numOfRows];
for(size_t i = 0 ; i < numOfRows; ++i)
{
array [ i ] = new T[numOfColumns];
}
}
So for your problem you can use it like this:
allocate2dArray(FileFrame,60,100000);
i would catch the bad_alloc exception to see if there was a failure.
try{ allocate2dArray(FileFrame,60,100000); }
catch(bad_alloc){...error code}
You can write a similar function to delete the array.
Edited by - felix9x on June 4, 2001 5:21:09 PM
Edited by - felix9x on June 4, 2001 5:27:37 PM
//allocate a two dimentional array on the heap
template < typename T >
void allocate2dArray(T**& array, int numOfRows, int numOfColumns)
{
array = new T*[numOfRows];
for(size_t i = 0 ; i < numOfRows; ++i)
{
array [ i ] = new T[numOfColumns];
}
}
So for your problem you can use it like this:
allocate2dArray(FileFrame,60,100000);
i would catch the bad_alloc exception to see if there was a failure.
try{ allocate2dArray(FileFrame,60,100000); }
catch(bad_alloc){...error code}
You can write a similar function to delete the array.
Edited by - felix9x on June 4, 2001 5:21:09 PM
Edited by - felix9x on June 4, 2001 5:27:37 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement