473,586 Members | 2,681 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 9038

"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
2347
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...
6
2499
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
1642
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...
10
3006
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
2315
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...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
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...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5390
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...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.