Very wierd problem

J

Johs32

I have the follwing code:

#include <stdio.h>
#include <stdlib.h>

struct data {
int *ip;
};


struct data first;
struct data *current = &first;


int init()
{
int a = 222;
current->ip = &a;
printf("init :%d\n", *current->ip);
return 0;
}
void zap(int a, int b)
{
printf("current->prio: %d\n",*current->ip);

}

int main()
{
init();
zap(3,5);
return 0;

}

When I do the printf in init() I get 222 as expected. But when I do it in
zap(int a, int b) I get 5!! If I remove the arguments from zap like this:

zap()

and call it like this from main:

zap()

I get 222 printed as expected from the zap() function.

What kind of black magic is this?

JOhs
 
E

Eric Sosman

Johs32 wrote On 03/14/06 15:02,:
I have the follwing code:
[snipped; see up-thread]

What kind of black magic is this?

The consequence of defective spells. If you haven't
been told already, there's a Frequently Asked Questions
(FAQ) site for comp.lang.c at

http://www.c-faq.com/

.... which you should visit, because the problem you're
experiencing is covered in Question 7.5a. (If you've
already been told to read the FAQ and you haven't done
so, shame on you!)

By the way, your problem is "weird" or possibly
"wired," but not "wierd."
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have the follwing code:

#include <stdio.h>
#include <stdlib.h>

struct data {
int *ip;
};


struct data first;
struct data *current = &first;


int init()
{
int a = 222;
current->ip = &a;
printf("init :%d\n", *current->ip);
return 0;
}
void zap(int a, int b)
{
printf("current->prio: %d\n",*current->ip);

}

int main()
{
init();
zap(3,5);
return 0;

}

When I do the printf in init() I get 222 as expected. But when I do it in
zap(int a, int b) I get 5!!

- From reviewing your code, I can see why.
[snip]
What kind of black magic is this?

There is no "black magic" involved. You just invoked "undefined
behaviour" by doing something incorrect in your code, that's all.

Tell me, at the printf() statement in init(), what does *current->ip
point to? What does it point to at the printf() statement in zap()? Is
there anything special about the object that *current->ip points to at
the point you get this odd behaviour?

Here's a hint: Unless you specify that the variable is "static",
variables defined within a function are only guaranteed to be available
/within/ the function that they were defined in, and they only last
until the end of the function. After that, any reference to them invokes
"undefined behaviour".

HTH
- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD4DBQFEFyZgagVFX4UWr64RArITAJ9wGtlihwpr7Bif8sOBF00/RvcfsQCYwWnd
b0lqTaSCbJvuiozW1KJnkw==
=5WY1
-----END PGP SIGNATURE-----
 
V

Vladimir S. Oka

I have the follwing code:

#include <stdio.h>
#include <stdlib.h>

struct data {
int *ip;
};


struct data first;
struct data *current = &first;


int init()
{
int a = 222;
current->ip = &a;
printf("init :%d\n", *current->ip);
return 0;
}
void zap(int a, int b)
{
printf("current->prio: %d\n",*current->ip);

}

int main()
{
init();
zap(3,5);
return 0;

}

When I do the printf in init() I get 222 as expected. But when I do it
in zap(int a, int b) I get 5!! If I remove the arguments from zap like
this:

zap()

and call it like this from main:

zap()

I get 222 printed as expected from the zap() function.

What kind of black magic is this?

No black magic, just poor programming (or poor understanding thereof).

In `init` you assign an address of a local variable to your pointer.
This local variable no longer exists once you exit `init`. Therefore,
dereferencing the pointer in `zap` reads memory that does not even
necessarily exist (as far as C Standard is concerned).

The difference you observe with or without parameters to `zap` is due to
the fact that your implementation seems to be using the stack, and the
same stack area for both functions. So, if you have no parameters it
does not bother changing the memory once occupied by `a` in `init`, but
with parameters to `zap`, one of them gets the honour of occupying the
same spot.

I suggest you go back and study local variables in C.
 
J

Johs32

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have the follwing code:

#include <stdio.h>
#include <stdlib.h>

struct data {
int *ip;
};


struct data first;
struct data *current = &first;


int init()
{
int a = 222;
current->ip = &a;
printf("init :%d\n", *current->ip);
return 0;
}
void zap(int a, int b)
{
printf("current->prio: %d\n",*current->ip);

}

int main()
{
init();
zap(3,5);
return 0;

}

When I do the printf in init() I get 222 as expected. But when I do it in
zap(int a, int b) I get 5!!

- From reviewing your code, I can see why.
[snip]
What kind of black magic is this?

There is no "black magic" involved. You just invoked "undefined
behaviour" by doing something incorrect in your code, that's all.

Tell me, at the printf() statement in init(), what does *current->ip
point to? What does it point to at the printf() statement in zap()? Is
there anything special about the object that *current->ip points to at
the point you get this odd behaviour?

I see your point. I have now moved "a" outside of the function:


int a = 222;
struct data first;
struct data *current = &first;

and changed init() to:

int init()
{
current->ip = &a;
printf("init :%d\n", *current->ip);
return 0;
}

And I know get the correct print form zap.
 
F

Fred Kleinschmidt

Johs32 said:
I have the follwing code:

#include <stdio.h>
#include <stdlib.h>

struct data {
int *ip;
};


struct data first;
struct data *current = &first;


int init()
{
int a = 222;
current->ip = &a;
printf("init :%d\n", *current->ip);
return 0;
}
void zap(int a, int b)
{
printf("current->prio: %d\n",*current->ip);

}

int main()
{
init();
zap(3,5);
return 0;

}

When I do the printf in init() I get 222 as expected. But when I do it in
zap(int a, int b) I get 5!! If I remove the arguments from zap like this:

zap()

and call it like this from main:

zap()

I get 222 printed as expected from the zap() function.

What kind of black magic is this?

JOhs

After returning from init(), current->ip points to an address that is now
out-of-scope.
(it was set to the address of variable 'a', which was local to init() now no
longer exists).
Anything can happen when you try to dereference it.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top