The following program contains a bug. Use gdb to identify the buggy codeline. De
ID: 3814242 • Letter: T
Question
The following program contains a bug. Use gdb to identify the buggy codeline. Describe the gdb commands you use in debugging and submit the line number of the bug. 1 #include 2 #include 3 int main(void) 4 {5 char base_digits(16) = 6 {'0', '1', '2', '3', '4', '5', '6', '7', 7 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 8 char *lastdigit = "F"; 9 int converted_number[64]; 10 long int number_to_convert; 11 int next_digit, base, index = 0; 12 number_to_convert = 12345; 13 base = 5; 14 while (number_to_convert ! = 0) 15 {16 converted_number [index] = number_to_convert % base; 17 number_to_convert = number_to_convert/base; 18 ++index; 19 *lastdigit = 'E'; 20} 21 --index; 22 printf (" Converted Number = "); 23 for (; index > = 0; index--) 24 {25 print ("%c", base_digits[converted_number[index]]); 26} 27}Explanation / Answer
Load executable into gdb:
Run program with run command:
You can find out where the program is, i.e. where the segmentation fault occurred, using the where command. This gives a function call trace of how you got to this point and shows line numbers inside files.
#include<stdio.h>
#include<string.h>
int main(void)
{
char base_digits[16]=
{'0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F'};
char *lastdigit="F";
int converted_number[64];
long int number_to_convert;
int next_digit,base,index=0;
number_to_convert=12345;
base=5;
while(number_to_convert!=0)
{
converted_number[index]=number_to_convert % base;
number_to_convert= number_to_convert / base;
++index;
*lastdigit="E";
}
--index;
printf(" Converted Number= ");
for( ; index>=0;index--)
{
printf("%c",base_digits[converted_number[index]]);
}
}
Line 19 has error. This assignment makes integer from pointer without a cast.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.