470,863 Members | 1,323 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,863 developers. It's quick & easy.

Undefined behaviour?

hello,

is this code has undefined behavour?

int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";

Why?

Thanks

Feb 8 '06 #1
8 1262
yu***@gmx.de wrote:
hello,

is this code has undefined behavour?

int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";

Why?

Thanks


It seems to be well-defined.

Ben
Feb 8 '06 #2
yu***@gmx.de wrote:
hello,

is this code has undefined behavour?
No.

int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";

Why?


Exactly.

Feb 8 '06 #3
In message <43**********************@news.optusnet.com.au>, benben
<be******@yahoo.com.au> writes
yu***@gmx.de wrote:
hello,
is this code has undefined behavour?
int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n"; Why?
Thanks


It seems to be well-defined.

But it's not a complete program. It's unlikely but possible that
something in the omitted part evilly defines a macro called "integer"
which completely changes the meaning of the above code ;-(

--
Richard Herring
Feb 8 '06 #4
posted:
hello,

is this code has undefined behavour?

int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";

Why?

Thanks


Seems like a trick question, so I'll give two answers.

Answer 1)

The code is OK, if you use it as follows:

include <iostream>
using std::cout;

int main()
{
int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";
}

Answer 2)

The code won't work. It lacks a function main(), and a definition of
"cout".
-Tomás
Feb 8 '06 #5

Tomás schrieb:
posted:
hello,

is this code has undefined behavour?

int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";

Why?

Thanks


Seems like a trick question, so I'll give two answers.

Answer 1)

The code is OK, if you use it as follows:

include <iostream>
using std::cout;

int main()
{
int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";
}

Answer 2)

The code won't work. It lacks a function main(), and a definition of
"cout".
-Tomás


Hello,
I am sorry, but I have seen that this code is having undefined
behaviour.
here:
http://groups.google.de/group/comp.l...ecf092d7c97e25

Regards

Feb 8 '06 #6
TB
yu***@gmx.de sade:
Tomás schrieb:
posted:
hello,

is this code has undefined behavour?

int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";

Why?

Thanks

Seems like a trick question, so I'll give two answers.

Answer 1)

The code is OK, if you use it as follows:

include <iostream>
using std::cout;

int main()
{
int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";
}

Answer 2)

The code won't work. It lacks a function main(), and a definition of
"cout".
-Tomás


Hello,
I am sorry, but I have seen that this code is having undefined
behaviour.
here:
http://groups.google.de/group/comp.l...browse_thread/
thread/144e5d30eb069ad5/9becf092d7c97e25#9becf092d7c97e25


Have you read the entire thread? Read what Kurt Stege's second
reply says.

--
TB @ SWEDEN
Feb 8 '06 #7
yu***@gmx.de wrote:
Hello, I am sorry, but I have seen that this code is having undefined
behaviour. here:
http://groups.google.de/group/comp.l...ecf092d7c97e25


You mean you've seen somebody *say* it has undefined behaviour:
int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";


And here you get an undefined behaviour, as you are dereferencing an
int ptr which points to a storage area which was not initialised as
an integer.


Of course, he is incorrect, since it *was* initialised.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 8 '06 #8
<yu***@gmx.de> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
hello,

is this code has undefined behavour?

int integer = 1234;
int * int_ptr = &integer;
cout << "*int_ptr = " << *int_ptr << "\n";

Why?

Thanks


That is not the same code that was in the message someone gave a link to.
The actual line that produced funky output was:
cout << "int_ptr = " << int_ptr << "\n";

which output
int_ptr = 0x804e03c

which is the value of the pointer.

Run this code:

#include <iostream>
int main()
{
int integer = 1234;
int * int_ptr = &integer;
std::cout << "*int_ptr = " << *int_ptr << std::endl;
}

your output should be
*int_ptr = 1234

isn't it?
Feb 9 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Simon Bailey | last post: by
8 posts views Thread by Scott J. McCaughrin | last post: by
8 posts views Thread by Joona I Palaste | last post: by
12 posts views Thread by RoSsIaCrIiLoIA | last post: by
26 posts views Thread by Frederick Gotham | last post: by
12 posts views Thread by Franz Hose | last post: by
10 posts views Thread by subramanian100in | last post: by
33 posts views Thread by coolguyaroundyou | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.