trap representation

J

junky_fellow

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.

thanks in advance ....
 
G

Guest

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.

thanks in advance ....

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?
 
H

Hallvard B Furuseth

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.
 
C

CBFalconer

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.
 
K

Keith Thompson

CBFalconer said:
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 (-32768). 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.)
 
J

junky_fellow

Keith said:
CBFalconer said:
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 (-32768). 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.)

Thanks everyone for your answers.
 
Joined
Jul 19, 2021
Messages
1
Reaction score
0
So,the trap representation means accessing or to say correctly using the values beyond the range of particular type is called trap representation right?. Sorry, I am wrong. Please help me to understand the correct one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top