No longer belongs to this program, but it still prints it - Strange

K

karthikbalaguru

Hi,

In the below code, the Gptr is pointing at where the variable 'a' used
to be, which no longer belongs to this program. So, it should fail.
But, it is printing '21' in VC++ 6.0. How is it possible ?

int *Gptr;
void TestGlobalPtr(void)
{
int a = 21;
Gptr = &a;
}
void main(void)
{
TestGlobalPtr();
printf("*Gptr = %d\n", *Gptr);
}

Any ideas ?

Thx in advans,
Karthik Balaguru . T
 
C

coder

karthikbalaguru said:
Hi,

In the below code, the Gptr is pointing at where the variable 'a' used
to be, which no longer belongs to this program. So, it should fail.
But, it is printing '21' in VC++ 6.0. How is it possible ?

Because the memory contents weren't yet overwritten?

Btw, since you're trying to access memory that the program has no
control over, this is undefined behavior.
 
R

Richard

karthikbalaguru said:
Hi,

In the below code, the Gptr is pointing at where the variable 'a' used
to be, which no longer belongs to this program. So, it should fail.

How do you know it no longer belongs to the "program"?
But, it is printing '21' in VC++ 6.0. How is it possible ?

I know you're trolling - 5/10 :-;

What happens if in main you do

*Gptr =0;

?
 
J

John Bode

Hi,

In the below code, the Gptr is pointing at where the variable 'a' used
to be, which no longer belongs to this program. So, it should fail.
But, it is printing '21' in VC++ 6.0. How is it possible ?

int *Gptr;
void TestGlobalPtr(void)
{
    int a = 21;
    Gptr = &a;}

void main(void)
{
    TestGlobalPtr();
    printf("*Gptr = %d\n", *Gptr);

}

Any ideas ?

Thx in advans,
Karthik Balaguru . T

You've invoked undefined behavior, which means the compiler can do
whatever it wants to with the code.

6.2.4.2: The lifetime of an object is the portion of program
execution during which storage is guaranteed to be
reserved for it. An object exists, has a constant address,
25)
and retains its last-stored value throughout its lifetime.
26)
If an object is referred to outside of its lifetime, the
behavior is undefined. The value of a pointer becomes
indeterminate when the object it points to reaches the end
of its lifetime.
 
K

karthikbalaguru

You've invoked undefined behavior, which means the compiler can do
whatever it wants to with the code.

6.2.4.2: The lifetime of an object is the portion of program
execution during which storage is guaranteed to be
reserved for it. An object exists, has a constant address,
25)
and retains its last-stored value throughout its lifetime.
26)
If an object is referred to outside of its lifetime, the
behavior is undefined. The value of a pointer becomes
indeterminate when the object it points to reaches the end
of its lifetime.

Thx for that info.

Karthik Balaguru
 
K

karthikbalaguru

Don't be too quick on this one; I've seen this user name before in the
engineering groups. It seems to be an account used by a classroom full of
students. Don't be surprised when the apparent skill level of the OP jumps
all over the place, it's different people.


No.
It does not belong to any classroom .
I asked these queries based on some code snippets
that i came across.

Karthik Balaguru
 
S

Stephen Sprunk

karthikbalaguru said:
Hi,

In the below code, the Gptr is pointing at where the variable 'a' used
to be, which no longer belongs to this program. So, it should fail.
But, it is printing '21' in VC++ 6.0. How is it possible ?

int *Gptr;
void TestGlobalPtr(void)
{
int a = 21;
Gptr = &a;
}
void main(void)
{
TestGlobalPtr();
printf("*Gptr = %d\n", *Gptr);
}

Returning the address of an automatic variable invokes undefined
behavior. "Undefined" is exactly that: there is no specific requirement
that _anything_ happen, including crashing.

S
 
D

Default User

karthikbalaguru wrote:

I asked these queries based on some code snippets
that i came across.

This is a poor way to learn C. Get a gook textbook and work your way
through it.




Brian
 
K

Keith Thompson

Default User said:
karthikbalaguru wrote:



This is a poor way to learn C. Get a gook textbook and work your way
through it.

I am sure, beyond reasonable doubt, that you meant to type "good".
 
A

Antoninus Twink

Get a gook textbook and work your way through it.

Ah, casual racism - nothing leaves a sour taste in the mouth quite like
it.

For what it's worth, I think Karthik is more likely to be an Indian name
than an Asian one.
 
D

Default User

Keith said:
I am sure, beyond reasonable doubt, that you meant to type "good".

Yes. On the bright side, I made myself laugh, and that's half the
battle.[1]



[1] Stolen from Scottish-American comedian and talk show host Craig
Ferguson.

Brian
 
D

Default User

Victor said:
<hm> Not Asian? Last time I looked India was still in Asia... </hm>

<aside> It's a hoot to be called a racist because of a typo </aside>

Easily explained, Twink is a troll. Although I've mentioned in the past
that I have killfiled him, he still likes to reply to my posts.




Brian
 
J

jameskuyper

Default User wrote:
....
Easily explained, Twink is a troll. Although I've mentioned in the past
that I have killfiled him, he still likes to reply to my posts.

Perhaps he doesn't believe you? For example, you've just revealed that
you know that he's responded to your messages. I suspect he's thick
enough to think that this revelation proves that you've actually been
reading those responses. He probably can't imagine that anyone would
ever stop paying attention to him. He does what appears to be his
level best to make reading his messages as unappealing as possible,
and then expects that we'll continue paying attention to them
indefinitely?
 
A

Antoninus Twink

<aside> It's a hoot to be called a racist because of a typo </aside>

Oh sure, a typo... after all, k and d are so close on the keyboard that
it's so easy to mix them up!

Freudian slip more like.
 
D

Default User

jameskuyper said:
Default User wrote:
...

Perhaps he doesn't believe you?

Possibly, but more likely he doesn't care.
For example, you've just revealed that
you know that he's responded to your messages.

Sure, I've seen his replies quoted in other's messages. Like Victor's.
I suspect he's thick
enough to think that this revelation proves that you've actually been
reading those responses.

The good thing is that I don't care what he thinks. Or doesn't, as the
case may be.
He probably can't imagine that anyone would
ever stop paying attention to him. He does what appears to be his
level best to make reading his messages as unappealing as possible,
and then expects that we'll continue paying attention to them
indefinitely?

I don't think I read any directly past his first day here.




Brian
 
J

Joachim Schmitz

Antoninus said:
Oh sure, a typo... after all, k and d are so close on the keyboard
that it's so easy to mix them up!

It is typed with the same finger, just the other hand...

Bye, Jojo
 
D

Default User

Joachim said:
Antoninus Twink wrote:

[blah blah]
It is typed with the same finger, just the other hand...

He knows that. He knows it's a typo. He's a damn troll. He'll say
whatever nonsense is necessary to get you to reply.





Brian
 
R

Richard

Default User said:
Joachim said:
Antoninus Twink wrote:

[blah blah]
It is typed with the same finger, just the other hand...

He knows that. He knows it's a typo. He's a damn troll. He'll say
whatever nonsense is necessary to get you to reply.





Brian

Another fine contribution Brian. I salute you. Han and Twink contribute
more in a day than you do in a year.

And you STILL have not learnt how to delimit your signature
properly. Please do so.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top