1

I was reading the Wikipedia article about Undefined behaviour.

in C the use of any automatic variable before it has been initialized yields undefined behavior

However, this answer says, it is ok for character types. Is Wikipedia wrong here?

Community
  • 1
  • 1
  • 2
    Where have you seen it says ok?It said legal but unspecified value. – MYMNeo Apr 17 '13 at 10:20
  • you know `lvalue`s and `rvalue`s ? Instead of trying to decrypt a page that anyone can edit read the standard. – user2244984 Apr 17 '13 at 10:22
  • 1
    From the standard: `If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.` – devnull Apr 17 '13 at 10:25
  • That answer doesn't say that at all. It even begins "Yes this behavior is undefined", then explains under what circumstances it isn't undefined (where the relevant part of `unsigned char` is the `unsigned`, not the `char`). – molbdnilo Apr 17 '13 at 10:28
  • The answer you refer to does not say it is okay to use character types. It says it is okay to use an uninitialized integer type of automatic storage duration **if its address is taken**. This requirement is new in C 2011. The reason is to allow for machines with special registers that cause traps if their contents are not initialized, even though the memory representation of an object does not have a trap representation. (Taking the address compels the compiler to put the object in memory instead of only in a register.) – Eric Postpischil Apr 17 '13 at 11:30

2 Answers2

4

In C (I have no idea about C++), the type unsigned char is the only type that guarantees all possible bit representations have a specific value. There is no trap representation, no invalid values, no padding bits, no nothing (as can be in other types).

However it is a bad idea to make a program that relies on an unknown bit pattern for some reason.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • In C++ (I have no idea about C), it's undefined for all types; any _lvalue_-to-_rvalue_ conversion of an uninitialised object gives undefined behaviour. – Mike Seymour Apr 17 '13 at 10:56
  • No, this changed in C 2011. Per C 2011 (n1570) 6.3.2.1 2: “If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.” – Eric Postpischil Apr 17 '13 at 11:25
0

Undefined behavior does not mean illegal or your program will crash here.

If you use an uninitialized variable (which happens if you allocate a primitive variable and don't assign a value to it, character types being one special kind of primitive types), the value is simply not determined. It can be anything. You might not bother the fact that it can be anything, because for example you could assign a value later, maybe only in some case.

However, when this becomes serious is when you read the value of the variable and make further decisions depending on this uninitialized value, for example in a condition:

int x;

if (x > 0) {
    ...
} else {
    ...
}

This will bring you here.

What the answer you linked says is that the following is perfectly fine:

int x;

if (someCase) {
    x = ...
} else {
    ...
}

// later:

if (someCase) {
    // read x
} 
Community
  • 1
  • 1
leemes
  • 44,967
  • 21
  • 135
  • 183
  • Although, technically, an uninitialized variable could contain a value that is "invalid for its type" or, in a system with ECC/parity, if the memory location has never been written, cause a "ECC" or "parity" error trap. This could cause all manner of nastiness. Yes, it's unlikely that this happens in your home PC with MacOS, Linux or Windows running on it. But C and C++ are languages that have the intention to run on almost any hardware in almost any environment. – Mats Petersson Apr 17 '13 at 11:02