Examing the Structure of an Object

1       #include <iostream.h>
2       class Shape {
3          public:
4               int x, y;
5               void Print() {
6                       cout << "Shape x,y " << x << " " << y << endl;
7               }
8       };
9
10      main() {
11              Shape s;
12              s.x = 3;
13              s.y = 4;
14              s.Print();
15      }
16
17
(gdb) run
Starting program: /home/randy/it

Breakpoint 1, Shape::Print (this=0xbffffca8) at it.cc:6
6                       cout << "Shape x,y " << x << " " << y << endl;
(gdb) p *this
$1 = {x = 3, y = 4}

Examing the Structure of an Object with a Virtual Function Table

1       #include <iostream.h>
2       class Shape {
3          public:
4               int x, y;
5               virtual void Print() {
6                       cout << "Shape x,y " << x << " " << y << endl;
7               }
8       };
9
10      main() {
11              Shape s;
12              s.x = 3;
13              s.y = 4;
14              s.Print();
15      }
16
17
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/randy/it

Breakpoint 1, Shape::Print (this=0xbffffca4) at it.cc:6
6                       cout << "Shape x,y " << x << " " << y << endl;
(gdb) p *this
$2 = {x = 3, y = 4, _vptr. = 0x804981c <Shape virtual table>}
(gdb)

Trying to make my own Virtual Function Table

[randy@euclid randy]$ g++ -g it.cc -o it
it.cc:5: variable or field `_vptr' declared void
it.cc:5: member `_vptr' conflicts with virtual function table field name

Examining The Code Produced (virtual example)

[randy@euclid randy]$ nm it
08048734 t .LFB2
08048794 t .LFB3
080487ac t .LFB4
080487aa t .LFE3
080487d6 t .LFE4
0804873b t .LM10
0804878c t .LM11
08048794 t .LM12
0804879a t .LM13
080487a8 t .LM14
080487ac t .LM15
080487d4 t .LM16
08048734 t .LM8
0804873b t .LM9
080487aa t .Lscope2
080487d6 t .Lscope3
08048734 W Print__5Shape
08049938 A _DYNAMIC
080498f8 A _GLOBAL_OFFSET_TABLE_
08048794 W __5Shape
080498ec ? __CTOR_END__
080498e8 ? __CTOR_LIST__
080498f4 ? __DTOR_END__
080498f0 ? __DTOR_LIST__
08049828 ? __EH_FRAME_BEGIN__
08049828 ? __FRAME_BEGIN__
080498e4 ? __FRAME_END__
080499d0 A __bss_start
08049810 D __data_start
         U __deregister_frame_info
08048704 t __do_global_ctors_aux
08048668 t __do_global_dtors_aux
080499d0 B __environ
         U __gmon_start__
         U __libc_init_first
         U __ls__7ostreamPCc
         U __ls__7ostreamPFR7ostream_R7ostream
         U __ls__7ostreami
         U __register_frame_info
         U __rtti_user
080487ac W __tf5Shape
         U __throw
08049a18 B __ti5Shape
0804981c W __vt_5Shape
080499d0 A _edata
08049a20 A _end
080499d0 W _environ
080487d6 A _etext
080487e0 ? _fini
08048520 ? _init
08048624 T _start
         U atexit
08049818 d completed.3
080499d8 B cout
08049810 W data_start
         U endl__FR7ostream
         U exit
080486ac t fini_dummy
0804981c d force_to_data
0804981c d force_to_data
080486b4 t frame_dummy
08048668 t gcc2_compiled.
08048668 t gcc2_compiled.
08048704 t gcc2_compiled.
08048734 t gcc2_compiled.
080486c8 t init_dummy
0804872c t init_dummy
080486d0 T main
08049a00 b object.8
08049814 d p.2