Page 1 of 1
Strange Function Call Error
Posted: Wed Sep 13, 2006 1:34 am
by spix
I'm having a strange problem.
I have a function call, which I pass a pointer and upon return the pointer has changed.
this is what i am doing:
Code: Select all
pointer c = 0x1000600c
call_function(c);
pointer c = 0x10000000
in call_function, right before the function returns, c = 0c1000600c
something happens between returning from "call_function" and getting back to the calling function.
I'm a bit lost. I tried disabling interrupts so no switching occurs, but it is the same.
I am using gcc 4.1.1 if that helps?
Andrew
[EDIT]
I just tried with gcc 3.4.4 and it works as it should, could this be a bug with 4.1.1? (I use -O3, if that matters)
[/EDIT]
Re:Strange Function Call Error
Posted: Wed Sep 13, 2006 2:03 am
by Pype.Clicker
ermh. wait a minute, when you say "right before the function returns, c = 0c1000600c", what you evaluate is the parameter given to "call_function", right? not the actual pointer which value changes?
e.g.
Code: Select all
char* actual = 0xc100600c;
void function(char* arg) {
// so, arg == 0xc100600c here, but what about actual?
}
If that's the case, the actual pointer might have its value changed anytime within the function. There are two things i could think of that would make your code behave like this:
1. you have some uninitialized pointer somewhere in "call_function" that makes you modify the value of actual (and possibly more things on stack, making your whole program completely unreliable).
2. your call_function is designed to return a structure, but the caller doesn't know that (e.g. because you're missing some function prototypes).
Re:Strange Function Call Error
Posted: Wed Sep 13, 2006 2:20 am
by spix
ermh. wait a minute, when you say "right before the function returns, c = 0c1000600c", what you evaluate is the parameter given to "call_function", right? not the actual pointer which value changes?
You're right. I just tested the actual value, and it is correct even when c changes.
Also, my function returns void, and the pointer is a pointer to a structure, which when I allocate, i memset it all to 0.
Andrew
Re:Strange Function Call Error
Posted: Wed Sep 13, 2006 2:42 am
by Pype.Clicker
so the only remaining explanation is an unitialized pointer somewhere, or maybe the structure is smaller than you thought? Check and double-check all your pointers declaration in your code and make sure you never have something like
Code: Select all
void* ptr; /* pointer initially contains garbage. It could operate on _anything_ */
or just run your code step-by-step, watching for the value of "actual" and see when it changes ...
Re:Strange Function Call Error
Posted: Wed Sep 13, 2006 4:17 am
by spix
You are right. Sorry, it was an uninitialized pointer. (3 of them actually.)
Thanks for the help.