This was a quiz (not graded) on Coursera. The question was, what does the following code possibly evaluate to? The correct answers were 127 and 0 (other options were crash, -1, 128. Why does the following code possibly evaluate to 0? I understand why it would evaluate to 127. Is it just as simple as the char bytes are uninitialized and therefore random? Can it also possibly evaluate to any # between 0 and 127?
int foo(void) {
char bar[128];
char *baz = &bar[0];
baz[127] = 0;
return strlen(baz);
}
Previously this answer had wrong information, this case does not invoke undefined behavior.
Edited answer:
TL;DR We cannot have a definitive answer, the code contains indeterministic behavior.
To elaborate, char bar[128];
is an automatic local variable and if not initialized explicitly, will contain indeterminate values.
Quoting C11
, chapter §6.7.9
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. [....]
In your code, you have assigned value for only one member of the array, at index 127. Remaining elements still have indeterminate value.
Attempt to pass that array (pointer to the first element of the array, basically) to strlen()
, causes a read on those values (in search of a null-terminator) and due to the indeterminate values, there's no guarantee that it will find the null-terminator at any particular location.
So, there's no definite answer for this question.
Note: (to make up for my wrong understanding to prevent readers from falling into the same trap further)
Here, reading the uninitialized values (i.e., indeterminate values) does not invoke undefined behaviour, as one may think.
The one liner: The address is taken for the object.
There's a detailed discussion on this topic, refer here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With