Function seems to re-initialize arguments

D

David Beers

I thought I understood C well enough, but this one has really taken me
by surprise.

I call a function that has several arguments. The function definition
happens to be in a different file (which I didn't think made a
difference). I can look at the value of the variables being passed
immediately before the function call and immediately after the
function begins to execute using my debugger. And the values
*change*.

Is there some strange scope rule that has eluded me so far in my C
programming? Is the stack being corrupted somehow? I'm at a loss.

My code is something like this (simplified, but honest to gosh, not
that much):

/* first.c */
void callingFunc()
{
int foo = 1;
int bar = 0;
int slug = 6000;

if (myFunc(foo, bar, &slug)) {
// do something
}
}

/* second.c */
int myFunc(int foo, int bar, int *slug)
{
long blah = 0;
// foo == 0 and bar == 44551 now!
// the address of slug is passed just fine, though
}

Any idea what could be happening here? (BTW, I'm programming on the
Palm OS, if this makes any difference.)

David
 
C

Christian Bau

I thought I understood C well enough, but this one has really taken me
by surprise.

I call a function that has several arguments. The function definition
happens to be in a different file (which I didn't think made a
difference). I can look at the value of the variables being passed
immediately before the function call and immediately after the
function begins to execute using my debugger. And the values
*change*.

Is there some strange scope rule that has eluded me so far in my C
programming? Is the stack being corrupted somehow? I'm at a loss.

My code is something like this (simplified, but honest to gosh, not
that much):

/* first.c */
void callingFunc()
{
int foo = 1;
int bar = 0;
int slug = 6000;

if (myFunc(foo, bar, &slug)) {
// do something
}
}

/* second.c */
int myFunc(int foo, int bar, int *slug)
{
long blah = 0;
// foo == 0 and bar == 44551 now!
// the address of slug is passed just fine, though
}

If that is actually how myFunc looks, that is it doesn't ever use foo,
bar and slug, then it is quite possible that an optimising compiler is
playing tricks on you. For example, the compiler decides that foo is
stored in register 1 and bar is stored in register 2, but because foo
and bar are never used, the compiler doesn't store the values that you
pass into registers 1 and 2. It doesn't have to, because your program
will work correct anyway.

Try printing the values in myFunc () using printf. If that doesn't work,
then you will need to investigate further.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top