Instructor: Professor Hong
https://classroom.github.com/g/1doHsCtp
due 12/11 @ 6PM - presentations
typedef int Length;
Length len, maxlen;
Length *lengths[];
typedef char *String;
String p, ineptr[MAXLINES], alloc(int);
int strcmp(String, String);
p = (String) malloc(100);
typedef struct tnode *Treeptr;
typedef struct tnode{ // the tree node:
char *word; // points to the text
int count; // number of occurrences
Treeptr left; // left child
Treeptr right; // right child
} Treenode;
Treeptr talloc(void)
{
return (Treeptr) malloc(sizeof(Treenode));
}
#include <stdio.h>
int main()
{
int a, b, c;
char d, e, f;
a = 1;
b = 2;
d = 'D';
e = 'E';
int *a_ptr, *b_ptr, *c_ptr;
char *d_ptr, *e_ptr, *f_ptr;
a_ptr = &a;
b_ptr = a_ptr;
c_ptr = b_ptr;
d_ptr = &d;
e_ptr = d_ptr;
f_ptr = d_ptr;
char *ptr = NULL; // Null pointer
printf("%c", *ptr);
return 0;
}
g++ -g segfault.cpp
gdb
(gdb) help
(gdb) file a.exe
(gdb) run
(gdb) continue
(gdb) break segfault.cpp:8
(gdb) run
(gdb) step
(gdb) next
(gdb) print a
(gdb) print a_ptr
(gdb) print *a_ptr
(gdb) print ptr
- NULL Pointer(gdb) print *ptr
- can't access!#include <stdio.h>
void sample_function()
{
int a, b, c;
a = 101;
b = 102;
char d, e, f;
d = 'd';
e = 'e';
return;
}
int main()
{
sample_function();
int a, b, c;
char d, e, f;
a = 1;
b = 2;
d = 'D';
e = 'E';
int *a_ptr, *b_ptr, *c_ptr;
char *d_ptr, *e_ptr, *f_ptr;
a_ptr = &a;
b_ptr = a_ptr;
c_ptr = b_ptr;
d_ptr = &d;
e_ptr = d_ptr;
f_ptr = d_ptr;
char *ptr = NULL; // Null pointer
printf("%c", *ptr);
return 0;
}
(gdb) break segfault_func.cpp:17
(gdb) step
#include <stdio.h>
void sample_function()
{
int a, b, c;
a = 101;
b = 102;
char d, e, f;
d = 'd';
e = 'e';
return;
}
int main()
{
sample_function();
int a, b, c;
char d, e, f;
a = 1;
b = 2;
d = 'D';
e = 'E';
a = 11;
a = 111;
int *a_ptr, *b_ptr, *c_ptr;
char *d_ptr, *e_ptr, *f_ptr;
a_ptr = &a;
b_ptr = a_ptr;
c_ptr = b_ptr;
d_ptr = &d;
e_ptr = d_ptr;
f_ptr = d_ptr;
char *ptr = NULL; // Null pointer
printf("%c", *ptr);
return 0;
}
gdb
(gdb) file a.exe
(gdb) break segfault_watch.cpp:17
(gdb) run
(gdb) watch a
- whenever a changes, it will print the old and new values and stop before the next line(gdb) next
breaktrace
- stack trace leading to a seg faultwhere
- same as backtrace, but in middle of program (e.g. in a function or in main)finish
- runs until current function finishesinfo breakpoints
- shows all declared breakpointsclear file:linenum
- deletes breakpointdelete
- deletes all breakpoints(gdb) break segfault_watch.cpp:27 if a >=100