Changing bool to a value other than 0 or 1?

I have the following code:

#include <stdio.h>

int main()
{
    bool b;

    // Set b to 123
    char *p = (char*)&b;
    *p = 123;

    // Check if b is considered to be true
    if (b == true) printf("b is true");

    return 0;
}

      

b

doesn't count true

, so what does true

it mean exactly, true

equal 1

?


Edit: Forgot to say that I am using Visual C ++.

+1


source to share


3 answers


bool b;

// Set b to 123
char *p = (char*)&b;
*p = 123;

      

The C ++ standard leaves unspecified how it bool

is stored ... eg. it is completely legal for one compiler to use one byte and the other to use four. In addition, one compiler can store words 0

for false

and all-bits-on for true

both 0

and 1

or any other value. All that mattered was that when used in a boolean context, the logic worked correctly and that when converted to or from int

:

  • int(b)

    1

    if b

    true

    and 0

    if b

    false

    and

  • bool(n)

    true

    if-and-only-if n

    not 0

    .

Because of this, the character with the smallest memory address in the representation bool

may or may not be considered when testing the boolean value: it can be a single compiler-generated code, uses a four-byte int

, which then only reads the least significant bit, or a byte which, depending on the entity may not have been affected *p = 123

. Likewise, the compiler can read a value into a signed integer register and test for negativity (expecting an "all bit" value for true

), which can also fail even if it *p = 123

only wrote one or the most significant byte.



Therefore, even if there was nothing else in the game - the line below may not say "b is true" ...

// Check if b is considered to be true
if (b == true) printf("b is true");

      

... but this test is even more flawed ...

  • it is possible to read from other bytes in the view bool

    that have not been initialized (uninitialized reads in memory have undefined behavior)

  • fiddling with a value bool

    essentially has "undefined behavior"; in terms of "binary writing" into memory bool

    , it is guaranteed that only a full byte byte copy from another properly initialized one bool

    will leave it bool

    usable.

+3


source


C ++ 11 says (3.9 "Types" [basic.types]):

An object value representation is a set of bits that contain a value of type T. For trivially copyable types, a value A representation is a set of bits in an object representation that defines a value, which is a single discrete element, an implementation-defined set of values

And (3.9.1 "Basic types" [basic.fundamental]):

For character types, all object representation bits are involved in the value representation. For unsigned character types, all possible bit patterns of value representation represent numbers. These requirements do not hold for other types .

...

Bool values ​​are either true or false

And finally, 5 "Expressions" [expr] say:



If, during evaluation of an expression, the result is not mathematically defined or is not in the range of displayable values for its type, the behavior is undefined.

What happens when you write the value 123 to the memory that an object occupies bool

(via char* p

), you write something that cannot be represented in the object bool

's value representation. "So when you subsequently access that object via an lvalue b

, the program exhibits undefined behavior and you have an object bool

that is neither true

, nor false

.

And to answer your question about what meaning is true

: the standard never specifies. It indicates that it true

is one of two values ​​that the type can represent bool

(the other is false

, of course). The standard also indicates that it true

easily converts to 1 and vice versa, 1 easily converts to true

. This transformation happens so naturally and easily that I think almost everyone thinks that true

1 matters.

Keep in mind that almost any non-zero integer value is easily converted to true

- not just 1. However, this does not happen in your example for 123, because in this expression it is written to an lvalue with a type char

, so there is no conversion to bool

.

+1


source


true

means 1

But 123 is also considered true (usually all integers except zero)

if you say

if (b) printf("b is true");

      

you should get output b is true

0


source







All Articles