Probably because the memset on line 721 is trashing memory.
Whenever you have a <= and an array, it's a pretty sure bet you really meant <, and that you've just stepped off the end of the array.
Plus, it's all so unnecessary as well.
What you should have had is just one of these in place of the memset
if (i == size) {
size += 5;
resizePlanets (pplanets, size);
}
*(*pplanets + i) = END_OF_PLANETS;
> *pplanets = (int *) malloc (size * 4);
1. Don't cast malloc in C http://c-faq.com/malloc/mallocnocast.html
2. Don't use literals for sizes - who said sizeof(int) was 4 ?
Do something like
*pplanets = malloc (size * sizeof(int));
Or better yet, use this pattern, which is guaranteed to be correct no matter what type of pointer p is p = malloc ( n * sizeof *p );
which in your case results in *pplanets = malloc (size * sizeof **pplanets);
> *pplanets = (int *) realloc (*pplanets, size * 4);
Classic realloc mis-use bug.
If realloc fails, it does NOT free the old memory.
But you just trashed your only pointer.
Do something like this
void *temp = realloc( *pplanets, size * sizeof **pplanets );
if ( temp != NULL ) {
*pplanets = temp;
} else {
// do something else, we ran out of memory
}
> *a ^= *b ^= *a ^= *b;
It's so cute - but totally wrong. http://c-faq.com/expr/xorswapexpr.html
Just try swapping a variable with itself and sit back and enjoy the fun.
The more obvious (and correct)
int temp = *a ; *a = *b ; *b = temp;
is far more likely to be spotted as a swap by a compiler, and treated as such if it is capable of doing something special.