Writing Past the End of an Array

When you reference an element in an array, the compiler (unfortunately) does not care whether the element actually exists. It computes how far past the first element it should go and then generates instructions that get or change that location's content. If the element is past the bounds of the array, this can be virtually anything and the results will be unpredictable. If you're lucky, your program will crash immediately or throw a bounds violation exception. If you're unlucky, you may get strange results much further on in your program run. You need to make sure that the condition in your for statement will cause the loop to stop before it goes past the bounds of the array. The condition expression Index <

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.