Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > trap representation

Reply
Thread Tools

trap representation

 
 
Mantorok Redgormor
Guest
Posts: n/a
 
      09-11-2003
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?
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      09-11-2003

"Mantorok Redgormor" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Glen Herrmannsfeldt
Guest
Posts: n/a
 
      09-11-2003

"Malcolm" <(E-Mail Removed)> wrote in message
news:bjqdan$ejh$(E-Mail Removed)...
>
> "Mantorok Redgormor" <(E-Mail Removed)> 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


 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-11-2003
"Glen Herrmannsfeldt" <(E-Mail Removed)> writes:

> "Malcolm" <(E-Mail Removed)> wrote in message
> news:bjqdan$ejh$(E-Mail Removed)...
> >
> > "Mantorok Redgormor" <(E-Mail Removed)> 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
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      09-11-2003
On Thu, 11 Sep 2003 19:08:19 +0100, "Malcolm" <(E-Mail Removed)>
wrote:

>
>"Mantorok Redgormor" <(E-Mail Removed)> 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')
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-11-2003
"j" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Fred L. Kleinschmidt
Guest
Posts: n/a
 
      09-11-2003


Lew Pitcher wrote:
>
> On Thu, 11 Sep 2003 19:08:19 +0100, "Malcolm" <(E-Mail Removed)>
> wrote:
>
> >
> >"Mantorok Redgormor" <(E-Mail Removed)> 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
 
Reply With Quote
 
j
Guest
Posts: n/a
 
      09-11-2003

"Ben Pfaff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Glen Herrmannsfeldt" <(E-Mail Removed)> writes:
>
> > "Malcolm" <(E-Mail Removed)> wrote in message
> > news:bjqdan$ejh$(E-Mail Removed)...
> > >
> > > "Mantorok Redgormor" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      09-11-2003

"Fred L. Kleinschmidt" <(E-Mail Removed)> 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.



 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      09-12-2003
"Malcolm" <(E-Mail Removed)> writes:

> "Mantorok Redgormor" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can "all bits zero" be a trap representation for integral types? Army1987 C Programming 6 07-07-2007 12:01 PM
Trap representation Richard Tobin C Programming 10 06-22-2007 11:42 PM
trap representation junky_fellow@yahoo.co.in C Programming 6 01-13-2007 05:47 AM
Trap representation ramu C Programming 2 01-31-2006 04:17 PM
trap representation pemo C Programming 11 12-14-2005 11:00 AM



Advertisments