The purpose of this lab is to use exception handling for memory allocation and to experiment with triggering bad allocation errors.
Copy your solution for Lab 2 (or the official solution: lab2.cpp) to lab7.cpp. Edit the file and replace the following line:
c = new StudentRecord[size];with:
try { c = new StudentRecord[size]; } catch(bad_alloc) { c = NULL; }Also remove the validation loop for size so that any size can be given to new.
Compile the code and try various invalid sizes such as negative numbers, zero and very large numbers (close to integer max of 2.14 billion). You should see the "Allocation failed" error message on most invalid sizes, except for zero (where Sleipnir fails to trigger an error).
Next, comment out the catch() block and run the program again. Notice how the
behavior changes when certain invalid sizes are given. When an exception is not
caught, the program will terminate "ungracefully".
Email your modified lab7.cpp code to me.