Jump to content

Uninitialized variable: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m top: created 1st section + complements
Line 22: Line 22:
What is the final value of <code>k</code>? The fact is, it is impossible to tell. The answer that it must be 10 assumes that it started at zero, which may or may not be true. Note that in the example, the variable <code>i</code> is initialized to zero by the first clause of the <code>for</code> statement.
What is the final value of <code>k</code>? The fact is, it is impossible to tell. The answer that it must be 10 assumes that it started at zero, which may or may not be true. Note that in the example, the variable <code>i</code> is initialized to zero by the first clause of the <code>for</code> statement.


In any case, even when a variable is ''implicitely'' initialised to a ''default'' value like 0, this is typically not the ''correct'' value. Initialised does not mean correct if the value is a default one. (However, default initialisation to 0 is a right practice for pointers and arrays of pointers, since it makes them invalid before they are actually initialised to their correct value.) In C, variables with static storage duration that are not initialized explicitly are initialized to zero (or null, for pointers).<ref>{{cite web| url=http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf| title=ISO/IEC 9899:TC3 (Current C standard)| page =126| accessdate=2008-09-26| date=2007-09-07| format=PDF}}
In any case, even when a variable is ''implicitly'' initialised to a ''default'' value like 0, this is typically not the ''correct'' value. Initialised does not mean correct if the value is a default one. (However, default initialisation to 0 is a right practice for pointers and arrays of pointers, since it makes them invalid before they are actually initialised to their correct value.) In C, variables with static storage duration that are not initialized explicitly are initialized to zero (or null, for pointers).<ref>{{cite web| url=http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf| title=ISO/IEC 9899:TC3 (Current C standard)| page =126| accessdate=2008-09-26| date=2007-09-07| format=PDF}}
Section 6.7.8, paragraph 10.</ref>
Section 6.7.8, paragraph 10.</ref>



Revision as of 14:05, 10 May 2014

In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.

Example of the C language

A common assumption made by novice programmers is that all variables are set to a known value, such as zero, when they are declared. While this is true for many languages, it is not true for all of them, and so the potential for error is there. Languages such as C use stack space for variables, and the collection of variables allocated for a subroutine is known as a stack frame. While the computer will set aside the appropriate amount of space for the stack frame, it usually does so simply by adjusting the value of the stack pointer, and does not set the memory itself to any new state (typically out of efficiency concerns). Therefore, whatever contents of that memory at the time will appear as initial values of the variables which occupy those addresses.

Here's a simple example in C:

void count( void )
{
    int k, i;
    
    for (i = 0; i < 10; i++)
    {
        k = k + 1;
    }
    
    printf("%d", k);
}

What is the final value of k? The fact is, it is impossible to tell. The answer that it must be 10 assumes that it started at zero, which may or may not be true. Note that in the example, the variable i is initialized to zero by the first clause of the for statement.

In any case, even when a variable is implicitly initialised to a default value like 0, this is typically not the correct value. Initialised does not mean correct if the value is a default one. (However, default initialisation to 0 is a right practice for pointers and arrays of pointers, since it makes them invalid before they are actually initialised to their correct value.) In C, variables with static storage duration that are not initialized explicitly are initialized to zero (or null, for pointers).[1]

Not only are uninitialized variables a frequent cause of bugs, but this kind of bug is particularly serious because it may not be reproducible: for instance, a variable may remain uninitialised only in some branch of the program. In some cases, programs with uninitialized variables may even pass software tests.

Use in languages

Uninitialized variables are a particular problem in languages such as assembly language, C, and C++, which were designed for systems programming. The development of these languages involved a design philosophy in which conflicts between performance and safety were generally resolved in favor of performance. The programmer was given the burden of being aware of dangerous issues such as uninitialized variables.

In other languages, variables are often initialized to known values when created. Examples include:

  • VHDL initializes all standard variables into special 'U' value. It is used in simulation, for debugging, to let the user to know when the don't care initial values, through the multivalued logic, affect the output.
  • Java does not have uninitialized variables. Fields of classes and objects that do not have an explicit initializer and elements of arrays are automatically initialized with the default value for their type (false for boolean, 0 for all numerical types, null for all reference types).[2] Local variables in Java must be definitely assigned to before they are accessed, or it is a compile error.
  • Python initializes local variables to NULL (distinct from None) and raises an UnboundLocalError when such a variable is accessed before being (re)initialized to a valid value.
  • D initializes all variables unless explicitly specified by the programmer not to.

Even in languages where uninitialized variables are allowed, many compilers will attempt to identify uninitialized variables and report them as compile-time errors.

References

  1. ^ "ISO/IEC 9899:TC3 (Current C standard)" (PDF). 2007-09-07. p. 126. Retrieved 2008-09-26. Section 6.7.8, paragraph 10.
  2. ^ "Java Language Specification: 4.12.5 Initial Values of Variables". Sun Microsystems. Retrieved 2008-10-18.