// This file contains the example program used in the gdb debugging // tutorial. The tutorial can be found on the web at // http://students.cec.wustl.edu/~agg1/tutorial/
#include<iostream.h>
int number_instantiated = 0;
template <classT> classNode { public: Node (const T &value, Node<T> *next = 0) : value_(value), next_(next) { cout << "Creating Node, " << ++number_instantiated << " are in existence right now" << endl; } ~Node () { cout << "Destroying Node, " << --number_instantiated << " are in existence right now" << endl; next_ = 0; }
// returns 0 on success, -1 on failure intinsert(const T &new_item){ return ((head_ = newNode<T>(new_item, head_)) != 0) ? 0 : -1; }
// returns 0 on success, -1 on failure intremove(const T &item_to_remove){ Node<T> *marker = head_; Node<T> *temp = 0; // temp points to one behind as we iterate
while (marker != 0) { if (marker->value() == item_to_remove) { if (temp == 0) { // marker is the first element in the list if (marker->next() == 0) { head_ = 0; delete marker; // marker is the only element in the list marker = 0; } else { head_ = newNode<T>(marker->value(), marker->next()); delete marker; marker = 0; } return0; } else { temp->next (marker->next()); delete temp; temp = 0; return0; } } marker = 0; // reset the marker temp = marker; marker = marker->next(); }
root@a852a26669ff:/bustub# gdb main GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty"for details. This GDB was configured as "aarch64-linux-gnu". Type "show configuration"for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type"help". Type "apropos word" to search for commands related to "word"... Reading symbols from main...done. (gdb)
(gdb) run Starting program: /bustub/main warning: Error disabling address space randomization: Operation not permitted Creating Node, 1 are in existence right now Creating Node, 2 are in existence right now Creating Node, 3 are in existence right now Creating Node, 4 are in existence right now The fully created list is: 4 3 2 1
Now removing elements: Creating Node, 5 are in existence right now Destroying Node, 4 are in existence right now 4 3 2 1
Program received signal SIGSEGV, Segmentation fault. 0x0000aaaac4c31304 in Node<int>::next (this=0x0) at main.cc:29 29 Node<T>* next () const { return next_; }
(gdb) backtrace #0 Node<int>::next (this=0x0) at main.cc:28 #1 0x2a16c in LinkedList<int>::remove (this=0x40160, item_to_remove=@0xffbef014) at main.cc:77 #2 0x1ad10 in main (argc=1, argv=0xffbef0a4) at main.cc:111 (gdb)
(gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y
Starting program: /home/cec/s/a/agg1/.www-docs/tutorial/main Creating Node, 1 are in existence right now Creating Node, 2 are in existence right now Creating Node, 3 are in existence right now Creating Node, 4 are in existence right now The fully created list is: 4 3 2 1
Now removing elements: Creating Node, 5 are in existence right now Destroying Node, 4 are in existence right now 4 3 2 1
Breakpoint 1, LinkedList<int>::remove (this=0x40160, item_to_remove=@0xffbef014) at main.cc:52 52 Node<T> *marker = head_; (gdb) step 53 Node<T> *temp = 0; // temp points to one behind as we iterate (gdb) 55 while (marker != 0) { (gdb) 56 if (marker->value() == item_to_remove) { (gdb) Node<int>::value (this=0x401b0) at main.cc:30 30 const T& value () const { return value_; } (gdb) LinkedList<int>::remove (this=0x40160, item_to_remove=@0xffbef014) at main.cc:75 75 marker = 0; // reset the marker (gdb) 76 temp = marker; (gdb) 77 marker = marker->next(); (gdb) Node<int>::next (this=0x0) at main.cc:28 28 Node<T>* next () const { return next_; } (gdb)
Program received signal SIGSEGV, Segmentation fault. Node<int>::next (this=0x0) at main.cc:28 28 Node<T>* next () const { return next_; } (gdb)