Note that there are some explanatory texts on larger screens.

plurals
  1. POOverflowing signed/unsigned assignments and its results
    primarykey
    data
    text
    <p>I'm reading a Stroustrup's book "The C++ Programming Language 4th edition" and have three questions regarding overflowing assignments (particularily to signed/unsigned chars, as the book exemplifies). Firstly, according to standard 5/4 paragraph:</p> <blockquote> <p>If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, <strong>the behavior is undefined</strong>.</p> </blockquote> <p>(Except when a destination variable is unsigned - the result is clearly defined in such case). But does this definition also pertain to assignments?</p> <p>Because in my opinion there are many contrary statements in the book, all in chapter 6. The first one corresponds to the aforementioned paragraph, however the following comments doesn't:</p> <blockquote> <p>Variables of the three <code>char</code> types can be freely assigned to each other. However, assigning a too large value to a <code>signed char</code> is still undefined. For example:</p> <pre><code>void g(char c, signed char sc, unsigned char uc) { c = 255; //implementation-defined if plain chars are signed and have 8 bits c = sc; //OK c = uc; //implementation-defined if plain chars are signed and if uc's value is too large sc = uc; //implementation-defined if uc's value is too large uc = sc; //OK: conversion to unsigned sc = c; //implementation-defined if plain chars are unsigned and if c's value is too large uc = c; //OK: conversion to unsigned } </code></pre> </blockquote> <p><strong>First question</strong>: since assigning a too large value is UB, then why comments say it's implementation-defined?</p> <p>Next we have the following example:</p> <blockquote> <p>To be concrete, assume that a <code>char</code> is 8 bits:</p> <pre><code>signed char sc = -160; unsigned char uc = sc; //uc == 116 (because 256-160==116) cout &lt;&lt; uc; //print 't' </code></pre> </blockquote> <p><strong>Second question</strong>: apart from the fact that the first assignment is supposedly UB, what formula exactly has the author used in order to come up with 116? In my test, <code>uc</code> got the value of 96.</p> <p>And the last quote:</p> <blockquote> <p>An integer can be converted to another integer type. If the destination is <code>signed</code>, the value is unchanged if it can be represented in the destination type; otherwise, the value is implementation-defined:</p> <p><code>signed char sc = 1023; //implementation-defined</code></p> <p>Plausible results are 127 and -1.</p> </blockquote> <p><strong>Third question</strong>: again, apart from the fact that this contrary to what have been said earlier about UB, why possible results are 127 and -1? I guess it has something to do with one's and two's complement, but what are the precise formulas used?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload