3

The elements of an uninitialized char array in C all have the value 'Ì' (or -52 as an integer; 0xCC or 204 as char). Is this another type of null character? Or is it just a value that fills in the values of an uninitialized char array?

(In case it makes any difference, I am using visual studio)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
PickleDonk
  • 75
  • 1
  • 8
  • 4
    This is a "junk" value. Like any other. Uninitialized array does not have any defined value. – Eugene Sh. Mar 02 '18 at 22:19
  • 3
    "Magic" junk values can be useful in debugging builds to help the runtime detect that you are accessing an uninitialized variable or buffer. But it can also just really be random junk. Ancient history: some compilers used to zero-fill buffers and variables for debug builds but not release builds, causing debug to work but release to crash. Fun! – Dave S Mar 02 '18 at 22:21
  • 1
    The reason they are all the same is because the VS Debug Heap manager initializes all uninitialized variables to "designer junk" for corruption detection. – Mark Benningfield Mar 02 '18 at 22:21
  • Note that in Hex it's 0xCC - a well recognized pattern. – Eugene Sh. Mar 02 '18 at 22:26
  • 2
    You are seeing the side-effect of the /RTC compile option, turned on by default for the debug build. It helps you detect that you are accessing an uninitialized local variable or an unterminated string. 0xcc is also very likely to cause an access violation for an uninitialized pointer or a debugger break if the program ran away. And it is used to detect buffer overflow. – Hans Passant Mar 02 '18 at 22:32
  • 1
    Possible duplicate of [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new) – Bo Persson Mar 02 '18 at 23:26

1 Answers1

3

Uninitialized variables have indeterminate values. They can make the behavior of your program undefined if the value is used to be a trap representation.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • A comment would be appreciated if the answer is addressing something wrong. – haccks Mar 02 '18 at 22:27
  • Can char have trap representation? – hyde Mar 02 '18 at 22:33
  • 1
    @hyde I think the standard is not explicitly forbidding it. – Eugene Sh. Mar 02 '18 at 22:35
  • For further reference on [using uninitialized variables see "Is uninitialized local variable the fastest random number generator?"](https://stackoverflow.com/a/31746063/1708801) – Shafik Yaghmour Mar 02 '18 at 23:06
  • 2
    @EugeneSh. In [c whether is has a trap representation changes between C99 and C11](http://blog.frama-c.com/index.php?post/2013/03/13/indeterminate-undefined) in C++ [unsigned narrow char types do not have trap rep](https://stackoverflow.com/a/23415662/1708801) – Shafik Yaghmour Mar 02 '18 at 23:11
  • @hyde [see my comment](https://stackoverflow.com/questions/49078237/what-does-the-value-%c3%8c-mean-in-c#comment85163479_49078254) – Shafik Yaghmour Mar 02 '18 at 23:11
  • Just as another question, if you were to put: `if (uninitializerArray[0] == 'Ì') ...`, would that be bad form, since 'Ì' is a "junk" value? – PickleDonk Mar 03 '18 at 00:46
  • 1
    @pickledonk: it would be incorrect. There is no unique "junk" value, and there is no way to tell that memory is uninitialized. – rici Mar 03 '18 at 02:18