I've been building this program to interface with the Swis ephemeris library.
It's working beautifully, except for one thing.
OK, I need to be able to specify it to retrieve information about multiple planets, such as longitude, speed, etc. So I have a function that parses the command line parameter for --planets and the like, and puts it into an array. The array is resized 5 elements at a time.
That all seems to be working fine, except that when I put it over 25 planets (i has stars and asteroids too, included), it starts having memory access errors. It's within the library, but I don't think it is the fault of the library since it is only when the program tries to retrieve information about more than 25 planets at once.
I'm going to attach the code. I really don't know which part of the code to post, but functions of interest include parsePlanets and printPlanets.
Thanks for any help.

Recommended Answers

All 2 Replies

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.

Hi,

Ah, thank you for those pointers (no pun intended, lol).

That's very interesting that one is not supposed to cast malloc.

concerning the memset, yeah that's just easier to set the end to END_OF_PLANETS, and so I've done that. I forget my reasoning for the <=, but obviously it was faulty.

concerning swap, didn't know that wasn't defined behavior. Thank you for that tip as well.

I'm still a little rusty in C (programmed in it a few years ago, but then went to other languages for a while), so yeah I'm still relearning.

This is working beautifully now, by the way. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.