473,246 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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 1335
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
8
by: Joona I Palaste | last post by:
We all know that this: void *p; if (p=malloc(1)) { free(p); p; } causes undefined behaviour if malloc() succeeds. But what about this?
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
12
by: RoSsIaCrIiLoIA | last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org> wrote: >"Romeo Colacitti" <wwromeo@gmail.com> writes: >> Chris Torek wrote: >>> In article <4205BD5C.6DC8@mindspring.com> >>>...
26
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work...
12
by: Franz Hose | last post by:
the following program, when compiled with gcc and '-std=c99', gcc says test.c:6: error: jump into scope of identifier with variably modified type that is, it does not even compile. ...
10
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.