In: Computer Science
1. compile and try to run broken.c
2. use the gdb debugger to try to find where the seg fault occurs
3. after a bit of detective work with the use of gdb, answer the questions in the file questions.txt
4. fix the error(s) in broken.c
****broken.c*****
/* This program creates an array containing 10 integers
and then at the bottom checks to see if the number 11
is in the array.
Note: the program contains a bug that only shows up when
it is run.
*/
#include <stdio.h>
#include <stdlib.h>
int main( void ) {
int i = 0, count = 0, search = 1, found = 0;
int number = 5;
int table[10];
table[0] = number;
printf( "table[%i]: %i\n", count, table[count] );
count = 1;
while( count < 10 ) {
table[count] = number++ * 2;
printf( "table[%i]: %i\n", count, table[count] );
count++;
}
while( search = 1 ) {
if( table[i++] == 11 ) {
search = 0;
found = i - 1;
}
if( count == i ) {
search = 0;
}
}
if( found )
printf( "The number 11 is in the table at location: %d\n", found
);
else
printf( "The number 11 is not in the table.\n" );
return 0;
}
questions.txt regarding broken.c
--------
broken.c
--------
2) What line in broken.c did the program seg fault on?
3) What was the value of the variable "count" at the time of the
seg fault?
4) What was the value of the variable "i" at the time of the seg
fault?
5) What was the value of the variable "search" at the time of the
seg fault?
6) What are the values in the "table" array?
7) what do you think the bug is?
1. When the program broken.c is run the following output is obtained:
A runtime error named segmentation fault is recorded since memory mapping to a location seemed empty.In this case the array table had table[9] as the max but the code tried to access table[10].
2)The error occurs on this line :
if( table[i++] == 11 ) where the value of i increments to 10 and the system tries to access table[10] which caused the segmentation fault.
3)The answers to questions.txt is yet below
4) The code with errors fixed :
#include <stdio.h>
#include <stdlib.h>
int main( void ) {
int i = 0, count = 0, search = 1, found = 0;
int number = 5;
int table[10];
table[0] = number;
printf( "table[%i]: %i\n", count, table[count] );
count = 1;
while( count < 10 ) {
table[count] = number++ * 2;
printf( "table[%i]: %i\n", count, table[count] );
count++;
}
while( search ==1 )
{ //the comparative
operator is == . The = operator is assignment operator, which will
always return
//1(true) and thus the loop would be infinite.
if( table[i++] == 11 ) {
search = 0;
found = i - 1;
}
if( count == i ) {
search =
0;
break;
//works without break
}
}
if( found )
printf( "The number 11 is in the table at location: %d\n", found
);
else
printf( "The number 11 is not in the table.\n" );
return 0;
}
output to this code:
Questions.txt:
2) On the line if( table[i++] == 11 ) as explained before.
3) count was 10 at the moment.
4) i was also 10 at the moment.
5)search was 1 at the moment
6) The values for array table are:
5 10 12 14 16 18 20 22 24 26 (total 10 data)
7)The bug was in the while loop.The comparing should have been like while(search==0) instead of while(search=0). Since the latter would result in infinite looping, looks like while(TRUE).
If you have any doubts pending mention them in the comments. If you have are satisfied with the answer please leave in a thumbs up, it really matters.
Thank You.
table[0]: 5 table[1]: 10 table [2] : 12 table [3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 Segmentation fault (core dumped) ... Program finished with exit code 139 Press ENTER to exit console.]
table[0]: 5 table[1]: 10 table[2]: 12 table[3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 The number 11 is not in the table. ... Program finished with exit code 0 Press ENTER to exit console.
table[0]: 5 table[1]: 10 table [2] : 12 table [3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 Segmentation fault (core dumped) ... Program finished with exit code 139 Press ENTER to exit console.]
table[0]: 5 table[1]: 10 table[2]: 12 table[3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 The number 11 is not in the table. ... Program finished with exit code 0 Press ENTER to exit console.