473,503 Members | 1,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very wierd problem

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
Mar 14 '06 #1
5 1662


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

--
Er*********@sun.com

Mar 14 '06 #2
On Tuesday 14 March 2006 20:02, Johs32 opined (in
<dv**********@news.net.uni-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?

Mar 14 '06 #3
-----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-----
Mar 14 '06 #4
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.


Mar 14 '06 #5

"Johs32" <df***@dsf.com> wrote in message
news:dv**********@news.net.uni-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.
Mar 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1986
by: vool | last post by:
Hi all, I've got a really wierd problem here. When I access a web page that adds information to a database on a PC running XP Pro it works as it should. When I access the same page from a...
3
2848
by: Markus Fischer | last post by:
Hi, I'm experiencing a wierd problem with IE 6 in Windows with two _slightly_ different Version. Give the following HTMl-Code, ideally the output of offsetTop should be "105"; a few pixel...
1
1225
by: cody | last post by:
I have a OOP problem with the well known pattern where objects containing an object which represents a list of subobjects. Now my problem is that the ctor of a subobject indirectly calls the...
1
1333
by: Dean | last post by:
PageOne.aspx has an image control. The imageURL of the image control points to ImagePump.aspx. ImagePump.aspx gets a bitmap out of a SQLServer database, puts in into a bitmap object and does a:...
1
1067
by: Flores Eken | last post by:
Hi group, I've made my own implementation of the Time Tracker ASP.NET starterkit, added several features and extra's, works like a charm. Now i've uploaded it to my hosting account, to be able...
7
2337
by: Shane Bishop | last post by:
I've been fighting with the Page_Load event firing twice. I looked through this user group and saw several other people having similar problems. There were various reasons for it:...
0
1093
by: Michael | last post by:
Hi, I found a wierd problem in DataGrid. If I set DataGrid's DataSource to empDataSet1 at designtime, then I can never change its DataSource at runtime, e.g., in the Button1_Click event...
112
3935
by: Tom | last post by:
This is very strange: I have a Windows Form with a Panel on it. In that panel I dynamically (at run time) create some labels, as so: for i=1 to x dim ctlNew as New Label() with ctlNew...
5
1706
by: desktop | last post by:
I am confused about the use of the template parameter "E" in the below class. Since when is it allowed to use these parameters like "E(1)" and what does it mean (where can I read more about this...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.