Velocity Reviews > trap representation

# trap representation

junky_fellow@yahoo.co.in
Guest
Posts: n/a

 01-12-2007
Guys,

The word "trap representation" has been used at many places in
standard C, but I never understood its meaning. I would be highly
grateful if someone could explain its meaning using some simple example
and simple language.

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a

 01-12-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Guys,
>
> The word "trap representation" has been used at many places in
> standard C, but I never understood its meaning. I would be highly
> grateful if someone could explain its meaning using some simple example
> and simple language.
>

A trap representation, simply speaking, is a bit pattern that does not
represent a value. If an int has 16 bits, and a range from -32767 to
+32767, there are 65536 possible bit patterns, and only 65535 possible
values, so you have one bit pattern more than you need to represent all
values. What do you do with this bit pattern?

Hallvard B Furuseth
Guest
Posts: n/a

 01-12-2007
(E-Mail Removed) writes:
> The word "trap representation" has been used at many places in
> standard C, but I never understood its meaning. I would be highly
> grateful if someone could explain its meaning using some simple
> example and simple language.

Roughly, it's a representation of a type T which causes undefined
behavior if you access or produce it _as an object type T_. If you
instead access it as e.g. an array of unsigned char, you are safe.
There are some other exceptions.

Actually I think the Standard does a good job of explaining it, even if
the standardese gets a bit convoluted. In C99 6.2.6 (Representations of
types), 6.2.6.1p5 says:

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.[41] Such a representation is called a trap
representation.

41) Thus, an automatic variable can be initialized to a trap
representation without causing undefined behavior, but the value
of the variable cannot be used until a proper value is stored in
it.

--
Hallvard

CBFalconer
Guest
Posts: n/a

 01-12-2007
"(E-Mail Removed)" wrote:
>
> The word "trap representation" has been used at many places in
> standard C, but I never understood its meaning. I would be
> highly grateful if someone could explain its meaning using some
> simple example and simple language.

For example, a 2's complement 16 bit machine could set MIN_INT to
-32767 and reserve the bit pattern 0x8000 as a trap representation
for uninitialized memory. Any int access that yielded that value
would cause a system trap.

Another case would be the inclusion of parity or ECC bits in the
actual storage. Under some cases those need to be visible to the
checking hardware. If they don't agree with the actual value in
the value bits, either the value is corrected (for ecc) or a memory
error is signalled (for parity). By having a way to access the
whole schmeer it becomes possible to build diagnostic code.

--
"I have a creative mind. You (singular) are eccentric.
He is insane. We are losing sight of reality.
You (plural) are smoking crack. They are certifiable."
Declension of verbs, per Lewin Edwards

Richard Heathfield
Guest
Posts: n/a

 01-12-2007
CBFalconer said:

<snip>

> For example, a 2's complement 16 bit machine could set MIN_INT

ITYM INT_MIN.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Keith Thompson
Guest
Posts: n/a

 01-12-2007
CBFalconer <(E-Mail Removed)> writes:
> "(E-Mail Removed)" wrote:
>>
>> The word "trap representation" has been used at many places in
>> standard C, but I never understood its meaning. I would be
>> highly grateful if someone could explain its meaning using some
>> simple example and simple language.

>
> For example, a 2's complement 16 bit machine could set MIN_INT to

(INT_MIN, as RH pointed out)

> -32767 and reserve the bit pattern 0x8000 as a trap representation
> for uninitialized memory. Any int access that yielded that value
> would cause a system trap.

[...]

Yes, that's one example, but the system trap is not necessary for it
to be considered a trap representation. In fact, the phrase "trape
representation" is a bit misleading. Attempting to access a trap
representation just causes undefined behavior.

For example, assuming 16-bit two's complement int, you could simply
define INT_MIN as (-32767) and INT_MAX as 32767, without changing
anything else. All operations (except those that refer to the INT_MIN
macro) will happen to work exactly as they would if INT_MIN were
defined as (-3276. But -32768 would magically become a trap
representation, and an attempt to access an int object with that value
would invoke undefined behavior -- not because it would cause any kind
of trap (it wouldn't), but simply because the implementation chooses
not to define the behavior.

(In the above, I misuse the term "value". I'm too lazy to fix the
wording, but you get the idea.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

junky_fellow@yahoo.co.in
Guest
Posts: n/a

 01-13-2007

Keith Thompson wrote:
> CBFalconer <(E-Mail Removed)> writes:
> > "(E-Mail Removed)" wrote:
> >>
> >> The word "trap representation" has been used at many places in
> >> standard C, but I never understood its meaning. I would be
> >> highly grateful if someone could explain its meaning using some
> >> simple example and simple language.

> >
> > For example, a 2's complement 16 bit machine could set MIN_INT to

>
> (INT_MIN, as RH pointed out)
>
> > -32767 and reserve the bit pattern 0x8000 as a trap representation
> > for uninitialized memory. Any int access that yielded that value
> > would cause a system trap.

> [...]
>
> Yes, that's one example, but the system trap is not necessary for it
> to be considered a trap representation. In fact, the phrase "trape
> representation" is a bit misleading. Attempting to access a trap
> representation just causes undefined behavior.
>
> For example, assuming 16-bit two's complement int, you could simply
> define INT_MIN as (-32767) and INT_MAX as 32767, without changing
> anything else. All operations (except those that refer to the INT_MIN
> macro) will happen to work exactly as they would if INT_MIN were
> defined as (-3276. But -32768 would magically become a trap
> representation, and an attempt to access an int object with that value
> would invoke undefined behavior -- not because it would cause any kind
> of trap (it wouldn't), but simply because the implementation chooses
> not to define the behavior.
>
> (In the above, I misuse the term "value". I'm too lazy to fix the
> wording, but you get the idea.)
>