Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please answer all parts, Thanks! Recall what causes segmentation fault and bus errors from lecture and the textbooks. Common cause is an invalid pointer or
Please answer all parts, Thanks!
Recall what causes segmentation fault and bus errors from lecture and the textbooks. Common cause is an invalid pointer or address that is being dereferenced by the C program. Use the program average.c for this exercise (at the bottom). The program is intended to find the average of all the numbers inputted by the user. Currently, it has a bus error if you input more than one number. Load average.c into GDB (or LLDB for Macintosh) with all the appropriate information and run it. GDB/LLDB will trap on the segmentation fault and give you back the prompt. First, find where the program executed ended by using backtrace (bt as shortcut) which will print out a stack trace. FInd the exact line that caused the segmentation fault. Answer the following questions below as well. 1. What line caused the segmentation fault? 2. How do you fix the line so it works properly? You can recompile the code and run the program again. The program now reads all the input values but the average calculated is still wrong. USE GDB TO FIX THE PROGRAM by looking at the output of read_values. To do this, either set a breakpoint using the line number or set a breakpoint in the read_values function. Then continue executing till the end of the function and view the values being returned. Answer the questions below. 3. What is the bug here? 4. How do you fix it? \#include / Read a set of values from the user. Store the sum in the sum variable and return the number of values read. // You CANNOT declare any global variables. // DO NOT change the definition of the read_values function when it comes to // adding/removing function parameters, or changing its return type. You may, // however, // Use GDB or LLDB to discover and fix errors. int read_values(double sum) \{ int values sum =0; printf("Enter input values (enter to finish): ); scanf("\%d", \&input); while (input 1=){ values+t; sum += input; scanf("\%d", input); return values; \} int main( ){ double sum =0; double sum values = read_values(sum); printf(" \ nAverage: \%g , sum/values); // Hint: How do we ensure that sum is updated here // AFTER read_values() manipulates it? return 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started