Question: Consider the following program #inelude do streap asing nanespace stdi const int HAXSIZE 16; statie int array[MAXSIZE statie int r/ bad idea too eall a
Consider the following program #inelude do streap asing nanespace stdi const int HAXSIZE 16; statie int array[MAXSIZE statie int r/ bad idea too eall a global variable i void initarray(int. int)i void printArraycint)a This initializes elenent areSl to val for each array elenent void inithrray int arrl]. int val) areli val; This prints the contents of the argument array, with each elenent printed as index: value" on its own line For exanple, a 4-elenent array containing (10,11,12,13) wouald print ass 0 10 2 12 3: 13 woid printhrray(int arrl int nain) int dummy instArrayarray. 5) i1l arrayi with tives int "array2aray: t ake a copy (clone) the array in a hew array arraylo1-99 arrayz21-8 / change the element [o1 of the arrayl / change the element [11 of the array printArray(azrayl): print out both arzays printhrrayt(array2): return 0 As you can see, the above program should initialize each element of the array arrayi to 3, update the 0th element to 9 and then print the entire array out. The array2 variable should be another array with the same contents, exeept without the update of the Oth element and an additional update of the 1st element. But the pregran printed this What keg expected; 0: 99 0: 99 After understanding the above code, finish writing printArray. Make sure to align columns see here for a good short description, or here for more details (part of this lab is to learn how to read documentation!) Run the program and you will get unexpected results. Understand why you get these results and explain in comments (be precise-your grade is based largely on your explanation). If you don't understand what is happening, use ddd to trace through the program while displaying the appropriate variables. 4: 5: 6: 8 7: 8 8: 7: 5 10: 11: 5 12: S 13 14: 15: 5 10: 11: 8 12: 8 13: 8 14: 15: Then, fix the code (with a minimal number of changes) to do as described above and run the program. 8: S 0: 99 More information (read this after you finish the above, as it won't make sense otherwise): One problem above 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! S: 0 8: 8 9: 8 18: 8 11: 8 12: 3: 8 14: 8 15: Submit the fixed program 9: 5 18: 5 12: 13
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
