I am trying to debug a program for segmentation fault, and did research online. The content I found did not seem to cover what I see, so I like to ask for help. Here is the output:
Code:
Program received signal SIGSEGV, Segmentation fault.
0x6f337044 in ?? ()
(gdb) backtrace
#0 0x6f337044 in ?? ()
#1 0x64713357 in ?? ()
#2 0x43756950 in ?? ()
#3 0x414a5377 in ?? ()
#4 0x6f6d5141 in ?? ()
#5 0x5a724453 in ?? ()
#6 0x30366330 in ?? ()
#7 0x2d6a747a in ?? ()
#8 0x52594e74 in ?? ()
#9 0x62685343 in ?? ()
#10 0x6e436e6d in ?? ()
#11 0x62455143 in ?? ()
#12 0x664b6f77 in ?? ()
#13 0x4f6a7134 in ?? ()
#14 0x485f5832 in ?? ()
#15 0x42386c31 in ?? ()
#16 0x674e436f in ?? ()
#17 0x775f7758 in ?? ()
#18 0x000a4263 in ?? ()
#19 0x0025c000 in ?? ()
Cannot access memory at address 0x5242354b
(gdb)
Here is the code:
Code:
1 #include <stdio.h>
2 int main ()
3 {
4 FILE *fp;
5 printf ("Enter any character please ");
6 getch();
7 fp = fopen ("somefile.txt", "r");
8 if (fp == NULL)
9 {
10 puts("Could not open the file");
11 exit(1);
12 }
13 int i, buffer;
14 char ch, line [200];
15 printf ("Press 'C/c' to continue or 'q/Q' to quite");
16 while ((ch=getch())=='c'|| ch == 'C')
17 {
18 for ( i=0; i<19; i++)
19 {
20 fgets(line, buffer, fp);
21 printf("%s", line);
22 }
23 if (ch == 'q' || ch == 'Q')
24 break;
25 if (ch != 'q' || ch != 'Q' || ch != 'c' || ch != 'C')
26 puts ("You did not provide a valid response, try again");
27 }
28 }
I think line 26 may mess things up. I tried to set a watch for 'ch' since for some reason on line 26 it has value of & in it! not sure how. I thought if I put a watch, then gdb would let me know when the value changes. But I did not get anything:-(
Here is the session output...
Code:
Breakpoint 1, main () at e6.c:23
23 if (ch == 'q' || ch == 'Q')
(gdb) n
25 if (ch != 'q' || ch != 'Q' || ch != 'c' || ch != 'C')
(gdb) p ch
$1 = 38 '&'
(gdb) help