Analysing more complex data types (GDB)

Code

#include <memory>
#include <vector>

class Pastery {
public:
  Pastery(const char* name) : m_Name(name) {}
  ~Pastery() { delete m_Name; }
private:
  const char* m_Name;
};

class Donut : public Pastery {
public:
  Donut(int radiusInCm) : Pastery("Donut"), m_RadiusInCm(radiusInCm) {}
private:
  int m_RadiusInCm;
};

class Brownie : public Pastery {
public:
  Brownie(bool hotCore) : Pastery("Brownie"), m_HotCore(hotCore) {}
private:
  bool m_HotCore;
};

int main() {
  using namespace std;

  unique_ptr<vector<Pastery*>> pastries(new vector<Pastery*>);
  pastries->push_back(new Donut(5));
  pastries->push_back(new Brownie(true));
  return 0;
}

Analysis

$ g++ main.cpp -g
$ gdb a.out
(gdb) b 32
(gdb) r
(gdb) print pastries._M_t->_M_head_impl
$5 = (std::vector<Pastery*, std::allocator<Pastery*> > *) 0x615c20
(gdb) p* (std::vector<Pastery*, std::allocator<Pastery*> > *) 0x615c20
$7 = std::vector of length 2, capacity 2 = {0x615c40, 0x615c80}
(gdb) p* (Donut*) 0x615c40
$8 = {<Pastery> = {m_Name = 0x4019de "Donut"}, m_RadiusInCm = 5}
(gdb) p* (Brownie*) 0x615c80
$10 = {<Pastery> = {m_Name = 0x4019e4 "Brownie"}, m_HotCore = true}
(gdb) q

Info: The used version of Python Pretty-Printer (used by GDB) couldn’t handle ‘print pastries’.