473,699 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trap representation

What does a trap representation mean in the standard?
And how can ~0 cause a trap representation? Could someone point out
the relevant sections in the standard?
Nov 13 '05 #1
18 9066

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
What does a trap representation mean in the standard?
And how can ~0 cause a trap representation? Could someone point
out the relevant sections in the standard?

A trap representaion is an illegal value for a variable to hold. The
platform detects such values, and then terminates the program with an error
message. For instance, a communist OS may decide that ascii dollar ($) is
illegal in strings. When you try to assign '$' to a char, it could terminate
with the error message "capitalist pig program".

All bits set is allowed as a trap value. Thus ~0 could potentially trap.
Nov 13 '05 #2

"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message
news:bj******** **@newsg4.svr.p ol.co.uk...

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
What does a trap representation mean in the standard?
And how can ~0 cause a trap representation? Could someone point
out the relevant sections in the standard?
A trap representaion is an illegal value for a variable to hold. The
platform detects such values, and then terminates the program with an

error message. For instance, a communist OS may decide that ascii dollar ($) is
illegal in strings. When you try to assign '$' to a char, it could terminate with the error message "capitalist pig program".

All bits set is allowed as a trap value. Thus ~0 could potentially trap.


Well, that could only be true with padding bits, or using other than twos
complement arithmetic.

The WATFIV Fortran compiler detected the use of undefined variables by
initializing all bytes to X'81' (0x81 in C) and then checking for that. It
worked pretty well, except for CHARACTER variables, in that it was pretty
unlikely to occur accidentally.

I believe that there may have been ones complement machines that would trap
on ~0, which is -0, but I wouldn't worry too much about them.

There was a suggestion not so long ago to detect twos complement machines
with:

#if ~0==-1

which would be compile time, and I have no idea what the preprocessor says
about traps.

#if ~1==-2

would work just as well, though.

-- glen
Nov 13 '05 #3
"Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> writes:
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message
news:bj******** **@newsg4.svr.p ol.co.uk...

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
What does a trap representation mean in the standard?
And how can ~0 cause a trap representation? Could someone point
out the relevant sections in the standard?

A trap representaion is an illegal value for a variable to hold. The
platform detects such values, and then terminates the program with an

error
message. For instance, a communist OS may decide that ascii dollar ($) is
illegal in strings. When you try to assign '$' to a char, it could

terminate
with the error message "capitalist pig program".

All bits set is allowed as a trap value. Thus ~0 could potentially trap.


Well, that could only be true with padding bits, or using other than twos
complement arithmetic.


C99 supports ones' complement and sign-magnitude as well as two's
complement, so portable code can't assume that two's complement
is in use.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Nov 13 '05 #4
On Thu, 11 Sep 2003 19:08:19 +0100, "Malcolm" <ma*****@55bank .freeserve.co.u k>
wrote:

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
What does a trap representation mean in the standard?
And how can ~0 cause a trap representation? Could someone point
out the relevant sections in the standard?
A trap representaion is an illegal value for a variable to hold.

[snip]All bits set is allowed as a trap value. Thus ~0 could potentially trap.


More specifically, on a ones-complement machine, all bits set is a potential
trap representation, as it represents the value you get when you evaluate
negative zero {(-0)}.

On a two's complement machine, -0 == all bits zero == 0
On a one's complement machine, -0 == all bits one != 0
On either machine ~0 == all bits one != 0

But the C abstract machine doesn't recognize a difference between 0 and -0, thus
making the one's complement expression a potential trap value ("cant happen").

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 13 '05 #5
j

"Ben Pfaff" <bl*@cs.stanfor d.edu> wrote in message
news:87******** ****@pfaff.stan ford.edu...
"Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> writes:
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message
news:bj******** **@newsg4.svr.p ol.co.uk...

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
> What does a trap representation mean in the standard?
> And how can ~0 cause a trap representation? Could someone point
> out the relevant sections in the standard?
>
A trap representaion is an illegal value for a variable to hold. The
platform detects such values, and then terminates the program with an

error
message. For instance, a communist OS may decide that ascii dollar ($) is illegal in strings. When you try to assign '$' to a char, it could

terminate
with the error message "capitalist pig program".

All bits set is allowed as a trap value. Thus ~0 could potentially
trap.
Well, that could only be true with padding bits, or using other than twos complement arithmetic.


C99 supports ones' complement and sign-magnitude as well as two's
complement, so portable code can't assume that two's complement
is in use.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman


Shouldn't that be sign-extended instead of one's complement? I don't think
one's complement is proper terminology.
Nov 13 '05 #6
"j" <ja****@bellsou th.net> writes:
Shouldn't that be sign-extended instead of one's complement? I don't think
one's complement is proper terminology.


See C99 6.2.6.2:

- the sign bit has the value -(2N - 1) (one's complement).

--
Bite me! said C.
Nov 13 '05 #7


Lew Pitcher wrote:

On Thu, 11 Sep 2003 19:08:19 +0100, "Malcolm" <ma*****@55bank .freeserve.co.u k>
wrote:

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
What does a trap representation mean in the standard?
And how can ~0 cause a trap representation? Could someone point
out the relevant sections in the standard?

A trap representaion is an illegal value for a variable to hold.

[snip]
All bits set is allowed as a trap value. Thus ~0 could potentially trap.


More specifically, on a ones-complement machine, all bits set is a potential
trap representation, as it represents the value you get when you evaluate
negative zero {(-0)}.

On a two's complement machine, -0 == all bits zero == 0
On a one's complement machine, -0 == all bits one != 0
On either machine ~0 == all bits one != 0

But the C abstract machine doesn't recognize a difference between 0 and -0, thus
making the one's complement expression a potential trap value ("cant happen").

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')


For integer types (signed or unsigned, short or long), would all ones be
an illegal value? I can see it for floats/doubles, but for integers???

On every platform I have encoutered with 16-bit shorts and 32-bit ints,
USHRT_MAX is 65535, which is sixteen ones in binary, and MAX_INT (or
MAX_INT) is 4294967295 which is 32 ones. For these cases, "all ones"
must be a legal value.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 13 '05 #8

"Fred L. Kleinschmidt" <fr************ *****@boeing.co m> wrote in

For integer types (signed or unsigned, short or long), would all ones be
an illegal value? I can see it for floats/doubles, but for integers???

It's more a theoretical problem than anything you are likely to encounter. A
machine doesn't have to use twos complement for negatives, and I believe it
is permitted to disallow all bits set for unsigned types also, so INT_MAX
might be 0xFFFE.

Nov 13 '05 #9
"Malcolm" <ma*****@55bank .freeserve.co.u k> writes:
"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
What does a trap representation mean in the standard?
6.2.6.1#5: Certain object representations need not represent a value
of the object type. If the stored value of an object has such a
representation and is read by an lvalue expression that does not have
character type, the behavior is undefined. If such a representation is
produced by a side effect that modifies all or any part of the object by
an lvalue expression that does not have character type, the behavior is
undefined. Such a representation is called a trap representation.
And how can ~0 cause a trap representation? Could someone point out
the relevant sections in the standard?

6.2.6.2#2: [...] Which of these [sign and magnitute, two's complement,
one's complement] applies is implementation-defined, as is whether the
value with sign bit 1 and all value bits zero (for the first two), or
with sign bit and all value bits 1 (for one's complement), is a trap
representation or a normal value. In the case of sign and magnitude and
one's complement, if this representation is a normal value it is called a
negative zero.
A trap representaion is an illegal value for a variable to hold. The
platform detects such values, and then terminates the program with an
error message.


Actually, the implementation is not required to detect a trap
representation. Using it causes undefined behavior.

Martin
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
437
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 the stored value of an object has such a representation and is read by an lvalue
10
2354
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 sure what 'a trap' means here - anyone?). I also read into this that, an *initialised* automatic variable, may never hold a bit pattern that might, when read, cause a 'trap'. I.e., if an auto is explicitly initialised, it may *never* hold a...
6
2506
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
1655
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, I'm asking wheter it equals 0 whenever uMyInt_t has trap representations, equals a nonzero value whenever uMyInt_t has no trap representation, and never triggers undefined behaviour.
10
3012
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 32 characters in some alphabets" - X3.4, 1963.
6
2327
by: Army1987 | last post by:
Reliable sources (whose names I'm not allowed to disclose) told me that on the next version of the Deathstation (version 10000, or DS10K) an integral type (they didn't tell which) will have a odd parity bit, that is a bit which is set if an even number of value/sign bits are set, and unset otherwise. If the parity is wrong, the representation is a trap, and they didn't tell me how it is handled (and they won't even have to tell this when...
0
8622
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8930
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7767
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4382
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3062
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.