### Program 8-4

posted Apr 10, 2013, 12:51 AM by Karwan Jacksi   [ updated Apr 10, 2013, 12:51 AM ]
 Program 8-41 // This program unsafely stores values beyond an array's boundary.2 // What happens depends on how your computer manages memory.3 // It MAY overwrite other memory variables. It MAY crash your computer.4 #include 5 using namespace std;67 int main()8 {9 const int SIZE = 3;10 int A[SIZE] = {1, 1, 1}; // Define A as a 3-element int array11 // holding the values 1, 1, 112 int B[SIZE]; // Define B as another 3-element int array1314 // Here is what is stored in array A15 cout << "Here are the original numbers in 3-element array A: ";16 for (int count = 0; count < 3; count++)17 cout << A[count] << " ";1819 // Attempt to store seven numbers in the 3-element array20 cout << "\n\nNow I'm storing 7 numbers in 3-element array B.";21 for (int count = 0; count < 7; count++)22 B[count] = 5;2324 // If the program is still running, display the numbers25 cout << "\nIf you see this message, the computer did not crash.";26 cout << "\n\nHere are the 7 numbers in array B : ";27 for (int count = 0; count < 7; count++)28 cout << B[count] << " ";2930 cout << "\nHere are the numbers now in array A: ";31 for (int count = 0; count < 3; count++)32 cout << A[count] << " ";3334 return 0;35 }Program OutputHere are the original numbers in 3-element array A: 1 1 1Now I'm storing 7 numbers in 3-element array B.If you see this message, the computer did not crash.Here are the 7 numbers in array B : 5 5 5 5 5 5 5Here are the numbers now in array A: 5 5 5