Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i'm getting a segmentation fault wh

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
0
0
GB
i'm getting a segmentation fault when unallocating memory using free.

int *array;

array = (int *)malloc(size);
..


..
if(array != NULL) {free(array);}

i'm doing a lot of stuff in between.

what conditions would give a seg fault here?

i should join this forum really!
 
what is the value of "size"? what is its data type?
 
sorry, size is allocated as following:

int size,nunmberofints;

numberofints = whatever;

size = sizeof(int) * numberofints;

 
Try using GDB to see if your problem really is in the free'ing. //Daniel
 
You have to really debug your code and see what the varaiable 'array' is assigned at the time of doing the malloc(), and check the value at the time of free().
If they are the same then you are most probably doing a double-free. If they are different then your variable has been corrupted by the code.
Try setting up breakpoints it will make the debugging much easier. I assumed that you have already seen the stack of the core dump and verified that the free() call is dumping core.
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
Or you could overrun the allocated space and assign data over its limit,i.e.
int *array = (int *)malloc(1000*sizeof(int))
array[1000] = something...

I think you'd get a segfault from that, at least on windows you would get something similar (its a long time from programming in Linux). [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
yeah, i didn't allocate the space properly - numberofints wasn't initialised properly.

when it came to free the space it fell over becuase of the memory overrun.

but why did it let the memory overrun in the first place?
 
yeah, i didn't allocate the space properly - numberofints wasn't initialised properly.

when it came to free the space it fell over becuase of the memory overrun.

but why did it let the memory overrun in the first place?
 
yeah, i didn't allocate the space properly - numberofints wasn't initialised properly.

when it came to free the space it fell over becuase of the memory overrun.

but why did it let the memory overrun in the first place?
 
hai,
after allocate the space if you are test like this
array=(int *)malloc(size);
if(array == NULL)
{
printf("memory is not allocated...");
exit(1);
}
i think this type of checking is usefull.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top