Strange Function Call Error

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
spix
Member
Member
Posts: 128
Joined: Mon Jun 26, 2006 8:41 am
Location: Millicent, South Australia
Contact:

Strange Function Call Error

Post 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]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Strange Function Call Error

Post 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).
User avatar
spix
Member
Member
Posts: 128
Joined: Mon Jun 26, 2006 8:41 am
Location: Millicent, South Australia
Contact:

Re:Strange Function Call Error

Post 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
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Strange Function Call Error

Post 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 ...
User avatar
spix
Member
Member
Posts: 128
Joined: Mon Jun 26, 2006 8:41 am
Location: Millicent, South Australia
Contact:

Re:Strange Function Call Error

Post by spix »

You are right. Sorry, it was an uninitialized pointer. (3 of them actually.)

Thanks for the help.
Post Reply