trap representation

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jan 12, 2007.

  1. 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 ....
     
    , Jan 12, 2007
    #1
    1. Advertising

  2. 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.
    >
    > 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?
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jan 12, 2007
    #2
    1. Advertising

  3. 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
     
    Hallvard B Furuseth, Jan 12, 2007
    #3
  4. CBFalconer Guest

    "" 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
     
    CBFalconer, Jan 12, 2007
    #4
  5. 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.
     
    Richard Heathfield, Jan 12, 2007
    #5
  6. CBFalconer <> writes:
    > "" 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 (-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.)

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jan 12, 2007
    #6
  7. Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    > > "" 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 (-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.
     
    , Jan 13, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    trap representation

    Mantorok Redgormor, Sep 11, 2003, in forum: C Programming
    Replies:
    18
    Views:
    715
  2. pemo

    trap representation

    pemo, Dec 5, 2005, in forum: C Programming
    Replies:
    11
    Views:
    545
    Tim Rentsch
    Dec 14, 2005
  3. ramu

    Trap representation

    ramu, Jan 31, 2006, in forum: C Programming
    Replies:
    2
    Views:
    342
    CBFalconer
    Jan 31, 2006
  4. Richard Tobin

    Trap representation

    Richard Tobin, Jun 22, 2007, in forum: C Programming
    Replies:
    10
    Views:
    577
    Keith Thompson
    Jun 23, 2007
  5. Army1987
    Replies:
    6
    Views:
    346
    CBFalconer
    Jul 7, 2007
Loading...

Share This Page