|
Assignment
Use TotalView to locate
an error in the following program. The program should print a sum of matrix components. The correct sum is 5.001E+7.
Details
cut and paste the code into the file wrong.c
icc -g -o wrong wrong.c
./wrong
Tips
- Load the program in TotalView.
- Double click on the data structure "mydata" in the stack frame
- In the window for the data structure "mydata", you will see a list of all the variables that make up the structure. Double click on veca2 to view the contents of the array.
- Click the "Step" button several times. Watch all the values in the array veca2. The value of the index "i" is visible in the stack frame window.
- When does the array veca2 become corrupt?
- What will fix the problem?
A solution
The index used to assign values to vecb in the first for loop causes it to go outside the boundaries of the array. The index only goes slightly outside the boundaries of the array, so it does not cause a segmentation fault. Instead, the loop starts assiging values to veca2. Values in veca2 were overwritten because veca2 was in the next chunk of memory following vecb.
Change the index for vecb (or remove the vecb assignment entirely) to solve the problem.
|