Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C char pointer length

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);

}
like image 206
Nathan Fowler Avatar asked Dec 14 '22 01:12

Nathan Fowler


1 Answers

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.

  • It can very well find a null terminator (ASCII value 0) in the very first element and return 0.
  • It can also not find any null terminator (ASCII value 0) in any of the other array elements until the last one and return 127.
  • It can find a null terminator anywhere in the array and return that count.

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.

like image 53
Sourav Ghosh Avatar answered Dec 30 '22 05:12

Sourav Ghosh