fixed segmention fault

abort allocation after first malloc fail
This commit is contained in:
Steffen Vogel 2013-10-07 17:22:53 +02:00 committed by Steffen Vogel
parent feec2b7de8
commit 16c65de934

View file

@ -62,17 +62,21 @@ int main(int argc, char** argv)
int i; int i;
for (i = 0; i < chunks; i++) { for (i = 0; i < chunks; i++) {
test[i] = malloc(size); test[i] = malloc(size);
if (!test) if (test[i])
printf("malloc(%d)\tFAILED!\n", chunks * sizeof(void *));
else
printf("malloc(%d)\tCHUNK: %d START: %p END: %p\n", size, i, test[i], test[i] + size); printf("malloc(%d)\tCHUNK: %d START: %p END: %p\n", size, i, test[i], test[i] + size);
else {
printf("malloc(%d)\tFAILED! Abort allocation, start with freeing memory\n", size);
break;
}
} }
// and release again // and release again
for (i = 0; i < chunks; i++) { for (i = 0; i < chunks; i++) {
if (test[i]) {
free(test[i]); free(test[i]);
printf("free(%p)\tCHUNK: %d\n", test[i], i); printf("free(%p)\tCHUNK: %d\n", test[i], i);
} }
}
free(test); free(test);
return 0; return 0;