473,387 Members | 1,501 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,387 software developers and data experts.

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

Similar topics

8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
11
by: pemo | last post by:
Ambiguous? I have a student who's asked me to explain the following std text (esp. the footnote). 6.2.6.1.5 Certain object representations need not represent a value of the object type. If...
10
by: pemo | last post by:
As far as I understand it, a trap representation means something like - an uninitialised automatic variable might /implicitly/ hold a bit-pattern that, if read, *might* cause a 'trap' (I'm not...
6
by: temper3243 | last post by:
Hi Can someone explain me what is happening below ? Why is it printing 401380 $ cat scanf.c #include<stdio.h> int main() { int i; scanf(" %d",&i);
17
by: Army1987 | last post by:
If uMyInt_t is an unsigned integral type, is the following a necessary and sufficient condition that uMyInt_t has no trap representation? (uMyInt_t)(-1) >CHAR_BIT*sizeof(uMyInt_t)-1 That is,...
10
by: Richard Tobin | last post by:
May all-bits-zero be a trap representation for a pointer? What if I calloc() space for a structure containing pointers? -- Richard -- "Consideration shall be given to the need for as many as...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.