i'm getting a segmentation fault when unallocating memory using free.
int *array;
array = (array *)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'm getting a segmentation fault when unallocating memory using free.
int *array;
array = (array *)malloc(size);
..
..
if(array != NULL) {free(array);}
i'm doing a lot of stuff in between.
what conditions would give a seg fault here?
sorry - should read
array = (int *)malloc(size);
using gdb the seg fault occurs at the free function!
ummm, is size an initialized variable? what happens when you try to free right after malloc ()?
hello, internet!
yep, size is initialised. the prog works fine withour the free - i just don't want any leaks.
if i put the free directly after the malloc its ok.
what could i be doing in between?
thanks salem, but its a variable size not the size of an int.
my problem is with free.
ehh? are you making an array of ints or one int? if its one int then itsOriginally posted by rotis23
thanks salem, but its a variable size not the size of an int.
my problem is with free.
malloc (sizeof (int))
if its an array of ints then its
malloc (sizeof (int) * numberofints)
nothing else makes sense for an int * variable.
hello, internet!
ok, the size has already been calculated.
int size,numberofints;
numberofints = whatever
size = sizeof (int) * numberofints;
Did you try to free the array twice?Originally posted by rotis23
i'm getting a segmentation fault when unallocating memory using free.
int *array;
array = (array *)malloc(size);
..
..
if(array != NULL) {free(array);}
i'm doing a lot of stuff in between.
what conditions would give a seg fault here?
Remember that the free function doesn't assign a NULL pointer to the array.
only 1 free for that array.
in nested loops i have the following twice:
array[array_count] = atoi(number);
array++;
array_count is initialised at start (before you ask):
int array_count = 0;
the array is used to check if it contains a certain number in a seperate function:
if(test_contain(array,number,array_count) == 1){...
int test_contain(int *arrayin,int numberin,int count) {
int i;
for(i=0;i<count;i++) {
if(arrayin[i] == numberin) {
return 0;
}
}
return 1;
}
sorry typed/copied it wrong. should be array_count++;
i'll try what you said. lot of code though.
i'm human alright..
... i make mistakes..
..ok, i admit it. i didn't set numberofints properly!!!!!!!!!!!!!!
back to basics for me. thanks anyway.