answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

Yes. All variables must be initialised (assigned a value) before they are first used. In a loop, a control variable (if specified) is "used" by the control statement. Using an uninitialised variable results in undefined behaviour, hence compilers will warn against this by default.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

It should be.

Good example: for (i=0; i<10; ++i)
Bad example: for (; i<10; ++i)

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is the loop control variable initialized after entering entering the loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between a do while loop and a for loop in c?

the counter variable cannot be initialized in while loop before entering into the block.


Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


A countdown loop operates by the loop control variable?

decremented


A loop control variable that is incremented a specific number of times is known as?

A loop control variable is widly known as a "counter".


What is the last step in a loop usually?

increment the loop control variable


When a VBScript loop control variable is not altered during loop execution what kind of loop may result?

An infinite loop.


Use the control variable during the execution of the loop?

Control variable which is used in control structures.e.g. for(int i=0;i


A variable declared inside the for loop control cannot be referenced outside the loop?

Yes. A variable declared inside the loop is a local variable for the code block enclosed by the {} statements of the for loop. The variable will not be available to be used by any code outside the code block.


Advantages of The Open Loop control?

Doing sex is an example of open loop so in this system u cannot control the amount of sperms entering your partner


How does a FOR loop work in GML?

FOR loops work as follows:{for( [initialize a variable]; [expression]; [increment the variable] ) {//Do this code}}Here as an example of a FOR loop:{for(i = 1; i < 10; i += 1) {show_message(string(i));}}What this will do is show a message 10 times displaying the value of "i" so you would get a message that says "1," another one after that saying "2," etc... The way this works is that you have the variable "i" initialized in the FOR loop. The FOR loop will keep looping until i >= 10, because the middle statement dictates that i must be smaller than 10 for the FOR loop activate. The third statement in the for loop is the statement that you increment the i variable with. If you change i += 1 to i -= 1 then the FOR loop would go on forever, freezing the game. This is a critical mistake to make when constructing a FOR loop (as is with any loop.)


What type of loop use a Boolean expression to control the number of times that it repeats a statement set of statements?

A counted loop. Typically we use a for loop for counted loops: // loop 10 times... for (int i=0; i&lt;10; ++i) { // ... } We can also use while and do-while loops to do the same thing, however a for loop provides all the information up front where it belongs and we can localise the control variable. With while and do-while loops, the control variable must be declared outside the loop, and the increment is usually specified at the end of the loop. This makes while and do-while loops harder to read because the information that controls the loop is separated: // loop 10 times... int i = 0; while (i&lt;10) { // ... ++i; } A do-while loop is similar to a while loop, but the control expression is placed at the end of the loop thus the loop always executes at least once. This also upsets the logic of a counted loop because the control variable is off-by-one. // loop 10 times... int i = 0; do { // ... ++i; } while (i&lt;11);


Which part of the for loop is resposible for assigning a value to a variable?

Typically, the variable is initialized in the first clause of the for statement, and incremented in the third clause. However, the language does not require any particular type of statement in these places.The following is a typical example:for (int i=0; i