Is the loop control variable initialized after entering entering the loop?

1 answer

Answer

1204450

2026-07-16 17:30

+ Follow

Uninitialised variables take on whatever value happens to reside at the memory address to which the variable was allocated at the point of instantiation. It is a "garbage value" because we cannot predict what the initial value will actually be at runtime. All variables must be initialised before they are used (read); if we use an uninitialised variable our program has undefined behaviour.

Ideally, initialisation should always occur at the point of instantiation. However, there can be valid reasons for delaying initialisation. For instance, when we read data from a disk file into a memory buffer, it doesn't make any sense to waste time initialising the buffer with values we're about to overwrite:

void read_file (std::ifstream& file) {

char buffer[1024]; // uninitialised!

while (file.good()) {

size_t index=0;

while (file.good() && index<1024) {

file >> buffer[index++]; // fill the buffer one character at a time

}

if (!file.good()) {

if (file.eof()) --index; // index is off-by-one, so adjust

else throw std::exception ("Unknown error in read_file()");

}

// use the buffer...

}

}

In the above example, index is used as a loop control variable in the inner loop and ultimately tells us how many characters were read into the buffer (such that index<=1024 upon exiting the inner loop). If EOF was reached, index will be off by one because we attempted to read one more character than actually exists, so we decrement accordingly. We could handle other read errors here but, for the sake of brevity, we throw an exception and let the caller deal with it. Assuming no read errors occurred, we can safely use the buffer (all indices less than index are valid). If the file contains more than 1024 characters, the next iteration of the outer loop will overwrite the buffer, so there's no need to flush it.

Now, consider what happens if we (accidently) fail to initialise index with the value 0:

void read_file (std::ifstream& file) {

char buffer[1024]; // uninitialised!

while (file.good()) {

size_t index; // WARNING: uninitialised

while (file.good() && index<1024) {

file >> buffer[index++]; // fill the buffer one character at a time

}

if (!file.good()) {

if (file.eof()) --index; // index is off-by-one, so adjust

else throw std::exception ("Unknown error in read_file()");

}

// use the buffer...

}

}

This code has undefined behaviour. Hope for a compiler warning!

If we suppose that the code compiles without warning (or we unwisely choose to ignore such warnings), there are several possible outcomes. Note that it is reasonable to assume that index will always be allocated the same address so, regardless of the initial value, it will hold whatever value was generated upon the previous iteration of the inner loop.

1. If index happens to hold the (correct) value zero, then the inner loop will read up to 1024 characters. However, any subsequent iteration will incur undefined behaviour because index would then be 1024 which is beyond the valid range of buffer.

2. If index happens to hold a value greater than zero but less than 1024, all characters before buffer[index] will remain uninitialised. Upon exiting the inner loop, there is no way to determine where writing began and index will tell us we've read more characters than were actually read. Any subsequent iteration will incur undefined behaviour because index would then be 1024 which is beyond the valid range of buffer.

3. If index happens to hold a value greater than or equal to 1024, the inner loop will never execute (nothing will be read from the file), thus file.good() can never become false and the outer loop becomes an infinite loop. Any attempt to read buffer[index] or any value beyond buffer[1023] incurs undefined behaviour.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.