Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Very wierd problem

Reply
Thread Tools

Very wierd problem

 
 
Johs32
Guest
Posts: n/a
 
      03-14-2006
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-14-2006


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."

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      03-14-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Johs32 wrote:
> 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

iD4DBQFEFyZgagVFX4UWr64RArITAJ9wGtlihwpr7Bif8sOBF0 0/RvcfsQCYwWnd
b0lqTaSCbJvuiozW1KJnkw==
=5WY1
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-14-2006
On Tuesday 14 March 2006 20:02, Johs32 opined (in
<dv77hh$hc6$(E-Mail Removed)-c.dk>):

> 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.

--
BR, Vladimir

Did YOU find a DIGITAL WATCH in YOUR box of VELVEETA?

 
Reply With Quote
 
Johs32
Guest
Posts: n/a
 
      03-14-2006
Lew Pitcher wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Johs32 wrote:
>> 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.




 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      03-14-2006

"Johs32" <(E-Mail Removed)> wrote in message
news:dv77hh$hc6$(E-Mail Removed)-c.dk...
>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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very wierd network problem redbull Computer Information 6 02-01-2005 04:13 PM
Quick Book file access very very very slow Thomas Reed Computer Support 7 04-09-2004 08:09 PM
Very wierd problem Bob Peterson Computer Support 5 02-08-2004 04:04 AM
Very wierd ASP.NET and SSL problem.. IE goes gray Flores Eken ASP .Net 1 12-26-2003 04:07 PM
Very wierd Problem. Please help. David Lucus ASP .Net Web Controls 4 07-10-2003 06:49 PM



Advertisments