Uninitialized variable

Uninitialized variable

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.

A common assumption made by novice programmers is that 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. 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 element of the for statement.

In C, variables with static storage duration that are not initialized explicitly are initialized to zero (or null, for pointers). [cite web| url=http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf| title=ISO/IEC 9899:TC3 (Current C standard)| pages 126| accessdate=2008-09-26| date=2007-09-07| format=PDFSection 6.7.8, paragraph 10.]

Not only are uninitialized variables a frequent cause of bugs, but this kind of bug is particularly serious because it may not be reproducible; in some cases, programs with uninitialized variables may even pass SQA.

Many modern compilers will attempt to identify uninitialized variables and report them as compile-time errors. This is a very valuable capability. It is important to read the compiler documentation carefully, as there may be unexpected limitations. For example, the warning may not be on by default; the warning may be issued only when generating optimized code; and the warning may not be reliable (it may fail to detect all cases of uninitialized variables, or it may issue warnings where variables are actually are initialized before use but only as a result of complex logic paths).

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 academically-designed languages, and languages intended for use by students, applications programmers, and other "mere mortals," variables are often initialized to known values when created.

Some languages, like Java, do not have uninitialized variables. In Java, 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). Local variables in Java must be definitely assigned to before they are accessed, or it is a compile error.

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Variable — A variable (pronEng|ˈvɛərɪəbl) is an attribute of a physical or an abstract system which may change its value while it is under observation. Examples include the height of a child, the temperature across a state, or the input to a function. This… …   Wikipedia

  • C variable types and declarations — The C programming language has an extensive system for declaring variables of different types. The rules for the more complex types can be confusing at times, due to the decisions taken over their design. The principal decision is that the… …   Wikipedia

  • Magic number (programming) — For other uses of the term, see Magic number (disambiguation). In computer programming, the term magic number has multiple meanings. It could refer to one or more of the following: A constant numerical or text value used to identify a file format …   Wikipedia

  • C Sharp syntax — The correct title of this article is C# syntax. The substitution or omission of the # sign is because of technical restrictions. Main article: C Sharp (programming language) This article describes the syntax of the C# programming language. The… …   Wikipedia

  • .bss — redirects here. For other uses, see BSS. In computer programming, .bss or bss (Block Started by Symbol) is used by many compilers and linkers as the name of the data segment containing static variables that are filled solely with zero valued data …   Wikipedia

  • Data segment — A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. This portion has a fixed size for each program depending upon the quantity of… …   Wikipedia

  • C data types — C Standard Library Data types Character classification Strings Mathematics File input/output Date/time Memory allocation …   Wikipedia

  • Iterator — In computer science, an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. An iterator is sometimes called a cursor, especially within the context of a… …   Wikipedia

  • Software bug — To report a MediaWiki error on Wikipedia, see Wikipedia:Bug reports. A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or …   Wikipedia

  • Nil — Contents 1 Computer programming 2 Music 3 Other 4 See also …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”