Wiki Markup |
---|
Local, automatic variables can assume unexpected values if they are used before they are initialized. C99 specifies, "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate" \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] (see also [bullet 10 | CC. Undefined Behavior#ub_10] of Appendix J). In the common case, on architectures that make use of a program stack, this value defaults to whichever values are currently stored in stack memory. While uninitialized memory often contains zeroes, this is not guaranteed. On architectures that include trap representations, reading an uninitialized object of any type other than {{unsigned char}} (i.e., including {{int}}) may trigger a trap (see [bullet 11 | CC. Undefined Behavior#ub_11] of Appendix J). Consequently, uninitialized memory can cause a program to behave in an unpredictable or unplanned manner, lead to [undefined behavior | BB. Definitions#undefined behavior], and may provide an avenue for attack. |
Additionally, memory allocated by functions such as malloc()
should not be used before being initialized as its contents are indeterminate.
In most cases, compilers warn about uninitialized variables. These warnings should be resolved as recommended by MSC00-C. Compile cleanly at high warning levels.Additionally, memory allocated by functions such as malloc()
should not be used before being initialized as its contents are indeterminate.
Noncompliant Code Example
...