Trap representations producing Undefined Behavior

T

thamizh.veriyan

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

kingfox

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

Bo Persson

(e-mail address removed) 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
 
G

grocery_stocker

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?
 
T

thamizh.veriyan

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.




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

thamizh.veriyan

(e-mail address removed) 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
 
T

thamizh.veriyan

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
 
B

Branimir Maksimovic

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.
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top