Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Uninitialized variable
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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 (programming language)|C]] use [[stack (data structure)|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 (computer programming)|pointer]], and does not set the [[computer storage|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: <syntaxhighlight lang="c"> void count(void) { int k; for (int i = 0; i < 10; i++) { k = k + 1; } printf("%d", k); } </syntaxhighlight> The final value of <code>k</code> is undefined. 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. Another example can be when dealing with [[struct]]s. In the code snippet below, we have a <code>struct student</code> which contains some variables describing the information about a student. The function <code>register_student</code> leaks memory contents because it fails to fully initialize the members of <code>struct student new_student</code>. If we take a closer look, in the beginning, <code>age</code>, <code>semester</code> and <code>student_number</code> are initialized. But the initialization of the <code>first_name</code> and <code>last_name</code> members are incorrect. This is because if the length of <code>first_name</code> and <code>last_name</code> character arrays are less than 16 bytes, during the <code>strcpy</code>,<ref name="Man7_strcpy"/> we fail to fully initialize the entire 16 bytes of memory reserved for each of these members. Hence after <code>memcpy()</code>'ing the resulted struct to <code>output</code>,<ref name="Man7_memcpy"/> we leak some stack memory to the caller. <syntaxhighlight lang="c"> struct student { unsigned int age; unsigned int semester; char first_name[16]; char last_name[16]; unsigned int student_number; }; int register_student(struct student *output, int age, char *first_name, char *last_name) { // If any of these pointers are Null, we fail. if (!output || !first_name || !last_name) { printf("Error!\n"); return -1; } // We make sure the length of the strings are less than 16 bytes (including the null-byte) // in order to avoid overflows if (strlen(first_name) > 15 || strlen(last_name) > 15) { printf("first_name and last_name cannot be longer than 16 characters!\n"); return -1; } // Initializing the members struct student new_student; new_student.age = age; new_student.semester = 1; new_student.student_number = get_new_student_number(); strcpy(new_student.first_name, first_name); strcpy(new_student.last_name, last_name); //copying the result to output memcpy(output, &new_student, sizeof(struct student)); return 0; } </syntaxhighlight> In any case, even when a variable is ''implicitly'' initialized to a ''default'' value like 0, this is typically not the ''correct'' value. Initialized does not mean correct if the value is a default one. (However, default initialization to [[Null pointer|0]] is a right practice for pointers and arrays of pointers, since it makes them invalid before they are actually initialized 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 name="ISO9899"/> 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 uninitialized only in some [[conditional (computer programming)|branch]] of the program. In some cases, programs with uninitialized variables may even pass [[software quality assurance|software test]]s.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)