Question: C++ , Find and fix the bug and Problem: #include using namespace std; const int MAXSIZE = 16; static int array1[MAXSIZE]; static int i; //

C++ , Find and fix the bug and Problem:

#include using namespace std; const int MAXSIZE = 16; static int array1[MAXSIZE]; static int i; // bad idea too call a global variable i! void initArray(int[], int); void printArray(int[]); // This initializes element arr[i] to val for each array element void initArray(int arr[], int val) { for (i=MAXSIZE; i >= 0; i--) arr[i] = val; return; } // This prints the contents of the argument array, with each element printed as "index: value" on its own line // For example, a 4-element array containing {10,11,12,13} would print as: // 0: 10 // 1: 11 // 2: 12 // 3: 13 void printArray(int arr[]) { ... }; int main() { int dummy; initArray(array1, 5); // fill array1 with fives int *array2 = array1; // make a copy (clone) the array in a new array array1[0]=99; // change the element [0] of the array1 array2[1]=888; // change the element [1] of the array2 // print out both arrays cout  

One problem resulted from a buffer overflow error. This is a common security error since it allows malicious code to access memory locations that they should not have access to. That memory location might have a function parameter, or even machine code for the program that can now be overwritten with malicious code!

C++ , Find and fix the bug and Problem: #include using namespace

Download the source file: https://www.dropbox.com/s/31qx8qykgeskjm6/10.cpp?dl=0

Partial FIX source: https://www.dropbox.com/s/161dtch2oe50b25/10-beta1.cpp?dl=0

But the program printed this: What we expected: 0: 99 1: 888 3: 5 8: 5 8: 5 11: 5 12: 5 11:0 12: 13: 5 15: 15: 0: 99 1: 888 1 888 8: 8: 5 11: 5 12: 5 12: 14:5 15: 15 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!