469,921 Members | 2,183 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Trap representations producing Undefined Behavior

Hi,

I am new to this community. I have a doubt regarding trap
representations.

I read in IBM's website that something like this is legal:

int main(){
int x=3;
{
int x=x;
}
}

It said there that the scope of x in the inner block begins after the
initialization. So during the initialization process, the latter x in
"int x=x" refers to x in the outer scope. I understood that to an
extent.

But someone told me that something like this can produce Undefined
Behavior:

int main(){
int x=3;
{
int x=x++;
}
}

Why does this code snippet exhibit Undefined Behavior? I was told that
here x++ can prduce trap representation. I wasnt able to understand
that. According to me, the x++ refers to x in the outer scope. So why
can't this return the current value of x (in the outer scope) i.e. 3
and also increment it?

I was also told that something like this is well defined.

int main() {
struct X {X operator++(int) {return X();} } x;
x=x++;
}

Can you please explain how?

Am sorry about asking so many questions. I thought they are all
related and decided to ask them here. Please forgive me if you think
they are not related. I would be grateful if you could help me
understand this better. Thanks.

May 12 '07 #1
7 1290
On 5月12日, 下午4时42分, thamizh.veri...@gmail.com wrote:
Hi,

I am new to this community. I have a doubt regarding trap
representations.

I read in IBM's website that something like this is legal:

int main(){
int x=3;
{
int x=x;
}

}

It said there that the scope of x in the inner block begins after the
initialization. So during the initialization process, the latter x in
"int x=x" refers to x in the outer scope. I understood that to an
extent.

But someone told me that something like this can produce Undefined
Behavior:

int main(){
int x=3;
{
int x=x++;
}

}

Why does this code snippet exhibit Undefined Behavior? I was told that
here x++ can prduce trap representation. I wasnt able to understand
that. According to me, the x++ refers to x in the outer scope. So why
can't this return the current value of x (in the outer scope) i.e. 3
and also increment it?
I don't know which compiler will make the latter x refer to outter
scope. I try three compiler (gcc, OpenWatcom, Borland C++ Builder),
they all treat the latter x as an uninitialized variable.

I was also told that something like this is well defined.

int main() {
struct X {X operator++(int) {return X();} } x;
x=x++;

}
x = x++ ==x = X(); so, it's well defined.

May 12 '07 #2
th*************@gmail.com wrote:
:: Hi,
::
:: I am new to this community. I have a doubt regarding trap
:: representations.
::
:: I read in IBM's website that something like this is legal:
::
:: int main(){
:: int x=3;
:: {
:: int x=x;
:: }
:: }
::
:: It said there that the scope of x in the inner block begins after
:: the initialization. So during the initialization process, the
:: latter x in "int x=x" refers to x in the outer scope. I understood
:: that to an extent.

No, that is not correct. The inner x comes into scope when it is fully
defined, which is just before the equal sign of the initialization. It is
then initialized by its own uninitialized value. Not too good!
Bo Persson
May 12 '07 #3
On May 12, 1:42 am, thamizh.veri...@gmail.com wrote:
Hi,

I am new to this community. I have a doubt regarding trap
representations.

I read in IBM's website that something like this is legal:

int main(){
int x=3;
{
int x=x;
}

}

It said there that the scope of x in the inner block begins after the
initialization. So during the initialization process, the latter x in
"int x=x" refers to x in the outer scope. I understood that to an
extent.

But someone told me that something like this can produce Undefined
Behavior:

int main(){
int x=3;
{
int x=x++;
}

}

Why does this code snippet exhibit Undefined Behavior? I was told that
here x++ can prduce trap representation. I wasnt able to understand
that. According to me, the x++ refers to x in the outer scope. So why
can't this return the current value of x (in the outer scope) i.e. 3
and also increment it?
I think
int x=x++;

gives Undefined Behavior because there isn't a sequence point. In this
case x is assigned to itself and increments x. So what would be the
final value of x in this case?

May 12 '07 #4
On May 12, 10:36*pm, kingfox <foxap...@gmail.comwrote:
On 5鏈12鏃, 涓嬪崍4鏃42鍒, thamizh.veri...@gmail.com wrote:
Hi,
I am new to this community. I have a doubt regarding trap
representations.
I read in IBM's website that something like this is legal:
int main(){
* *int x=3;
* *{
* * * int x=x;
* *}
}


I don't know which compiler will make the latter x refer to outter
scope. I try three compiler (gcc, OpenWatcom, Borland C++ Builder),
they all treat the latter x as an uninitialized variable.
I was also told that something like this is well defined.
int main() {
struct X {X operator++(int) {return X();} } x;
x=x++;
}

x = x++ ==x = X(); so, it's well defined.- Hide quoted text -

- Show quoted text -
I tried it on Dev C++ which uses mingw and also tried it on GCC. It is
initializing the inner 'x' with the one in the outer scope. However
since you are getting different values I guess that snippet exhibits
undefined behavior too. However do you have anything from the standard
that explains this?

And it is funny that IBM has mentioned this snippet as an example for
explaining scope in XL C/C++ Documentation.

May 13 '07 #5
On May 13, 12:33 am, "Bo Persson" <b...@gmb.dkwrote:
thamizh.veri...@gmail.com wrote:

:: Hi,
::
:: I am new to this community. I have a doubt regarding trap
:: representations.
::
:: I read in IBM's website that something like this is legal:
::
:: int main(){
:: int x=3;
:: {
:: int x=x;
:: }
:: }
::
:: It said there that the scope of x in the inner block begins after
:: the initialization. So during the initialization process, the
:: latter x in "int x=x" refers to x in the outer scope. I understood
:: that to an extent.

No, that is not correct. The inner x comes into scope when it is fully
defined, which is just before the equal sign of the initialization. It is
then initialized by its own uninitialized value. Not too good!

Bo Persson
Hi Bo,

If that is the case then compilers like mingw which is GCC's port to
windows are not standard conformant because on these compilers the
value of x in the inner scope is initialized with the one in outer
scope.

If what you said is right, then the statement intx=x; as such is not
wrong because it should be equivalent to int x; Only when you attempt
to use this x without initializing x, your code would produce
Undefined Behavior.

So just to understand better, are you saying that the statement
intx=x; is likely to cause Undefined Behavior if x is used without
assigning any meaningful value to it, or are you saying that the
statement int x=x; itself produces Undefined Behavior.

Please help me understand.

Cheers
Madhu

May 13 '07 #6
On May 13, 12:54 am, grocery_stocker <cdal...@gmail.comwrote:
On May 12, 1:42 am, thamizh.veri...@gmail.com wrote:


Hi,
I am new to this community. I have a doubt regarding trap
representations.
I read in IBM's website that something like this is legal:
int main(){
int x=3;
{
int x=x;
}
}
It said there that the scope of x in the inner block begins after the
initialization. So during the initialization process, the latter x in
"int x=x" refers to x in the outer scope. I understood that to an
extent.
But someone told me that something like this can produce Undefined
Behavior:
int main(){
int x=3;
{
int x=x++;
}
}
Why does this code snippet exhibit Undefined Behavior? I was told that
here x++ can prduce trap representation. I wasnt able to understand
that. According to me, the x++ refers to x in the outer scope. So why
can't this return the current value of x (in the outer scope) i.e. 3
and also increment it?

I think
int x=x++;

gives Undefined Behavior because there isn't a sequence point. In this
case x is assigned to itself and increments x. So what would be the
final value of x in this case?- Hide quoted text -

- Show quoted text -
Hi Grocery,

I agree that it would produce Undefined Behavior if both the x's
referred to the same object. No question about that. My question
however is whether the x++ would refer to the x in the inner scope or
the one in the outer scope. I may sound stupid to be asking this again
and again, but I still havent got an answer which has explained the
concepts clearly. If you can point me to any section in the standards,
that would be great too.

Cheers

May 13 '07 #7
On May 12, 10:42 am, thamizh.veri...@gmail.com wrote:
Hi,

I am new to this community. I have a doubt regarding trap
representations.

I read in IBM's website that something like this is legal:

int main(){
int x=3;
{
int x=x;
}

}

It said there that the scope of x in the inner block begins after the
initialization. So during the initialization process, the latter x in
"int x=x" refers to x in the outer scope. I understood that to an
extent.
Either you interpreted this wrong or web site is plain
wrong. Since you want citation from standard, here is one:

"
3.3.1 Point of declaration [basic.scope.pdecl]
1 The point of declaration for a name is immediately after its
complete declarator (clause 8) and before its
initializer (if any), except as noted below. [Example:
int x = 12;
{ int x = x; }
Here the second x is initialized with its own (indeterminate) value. ]
"
Greetings, Branimir.

May 13 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by pemo | last post: by
10 posts views Thread by pemo | last post: by
6 posts views Thread by temper3243 | last post: by
17 posts views Thread by Army1987 | last post: by
10 posts views Thread by Richard Tobin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.