Uninitialised variables take on whatever value happens to resideat the memory address to which the variable was allocated at thepoint of instantiation. It is a "garbage value" because we cannotpredict what the initial value will actually be at runtime. Allvariables must be initialised before they are used (read); if weuse an uninitialised variable our program has undefinedbehaviour.Ideally, initialisation should always occur at the point ofinstantiation. However, there can be valid reasons for delayinginitialisation. For instance, when we read data from a disk fileinto a memory buffer, it doesn't make any sense to waste timeinitialising 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 characterat a time}if (!file.good()) {if (file.eof()) --index; // index is off-by-one, so adjustelse throw std::exception ("Unknown error in read_file()");}// use the buffer...}}In the above example, index is used as a loop control variablein the inner loop and ultimately tells us how many characters wereread into the buffer (such that index<=1024 upon exiting theinner loop). If EOF was reached, index will be off by one becausewe attempted to read one more character than actually exists, so wedecrement accordingly. We could handle other read errors here but,for the sake of brevity, we throw an exception and let the callerdeal with it. Assuming no read errors occurred, we can safely usethe buffer (all indices less than index are valid). If the filecontains more than 1024 characters, the next iteration of the outerloop will overwrite the buffer, so there's no need to flush it.Now, consider what happens if we (accidently) fail to initialiseindex with the value 0:void read_file (std::ifstream& file) {char buffer[1024]; // uninitialised!while (file.good()) {size_t index; // WARNING: uninitialisedwhile (file.good() && index<1024) {file >> buffer[index++]; // fill the buffer one characterat a time}if (!file.good()) {if (file.eof()) --index; // index is off-by-one, so adjustelse throw std::exception ("Unknown error in read_file()");}// use the buffer...}}This code has undefined behaviour. Hope for a compilerwarning!If we suppose that the code compiles without warning (or weunwisely choose to ignore such warnings), there are severalpossible outcomes. Note that it is reasonable to assume that indexwill always be allocated the same address so, regardless of theinitial value, it will hold whatever value was generated upon theprevious iteration of the inner loop.1. If index happens to hold the (correct) value zero, then theinner loop will read up to 1024 characters. However, any subsequentiteration will incur undefined behaviour because index would thenbe 1024 which is beyond the valid range of buffer.2. If index happens to hold a value greater than zero but lessthan 1024, all characters before buffer[index] will remainuninitialised. Upon exiting the inner loop, there is no way todetermine where writing began and index will tell us we've readmore characters than were actually read. Any subsequent iterationwill incur undefined behaviour because index would then be 1024which is beyond the valid range of buffer.3. If index happens to hold a value greater than or equal to1024, the inner loop will never execute (nothing will be read fromthe file), thus file.good() can never become false and the outerloop becomes an infinite loop. Any attempt to read buffer[index] orany value beyond buffer[1023] incurs undefined behaviour.