This section, look at the layout of the iterator object in memory in the list
1 #include <list> 2 3 void init (std::list<int>& lst) 4 { 5 for (int i = 0; i < 0 X10; i++) 6 { 7 lst.push_back (i); 8 } 9} ten int getsum (std::list<int>& lst) ( std::list<int>::iterator iter) ; int result = 0; (iter = Lst.begin (); ITER! = Lst.end (); iter++) ( +) + result + = *iter; ( ) + return result; () the ( ). int main (), { std::list<int> lst, init (l ST); getsum return (LST); 30}
Look at the assembly of the Getsum function:
(GDB) disassemble getsumdump of assembler code for function _Z6GETSUMRST4LISTIISAIIEE:0X080486CD <+0>:p ush%EBP 0x080486ce <+1>:mov%esp,%ebp 0x080486d0 <+3>:sub $0x38,%esp 0x080486d3 <+6>:lea-0x18 (% EBP),%eax 0x080486d6 <+9>:mov%eax, (%ESP) 0x080486d9 <+12>:call 0x8048816 <_znst14_list_iteratorii Ec2ev> 0x080486de <+17>:movl $0x0,-0xc (%EBP) 0x080486e5 <+24>:lea-0x1c (%EBP),%eax 0x080486e8 &L T;+27>:mov 0x8 (%EBP),%edx 0x080486eb <+30>:mov%edx,0x4 (%esp) 0x080486ef <+34>:mov%eax, (%ESP) 0x080486f2 <+37>:call 0x8048824 <_ZNSt4listIiSaIiEE5beginEv> 0x080486f7 <+42>:sub $0x4,%esp 0x080486fa <+45>:mov-0x1c (%EBP),%eax 0x080486fd <+48>:mov%eax,-0x18 (%EBP) 0x08048700 <+51>:j MP 0x804872f <_Z6getSumRSt4listIiSaIiEE+98> 0x08048702 <+53>:lea-0x18 (%EBP),%eax 0x08048705 <+56 >:mov%eax, (%ESP) 0x08048708 <+59>:call 0x80488ba <_ZNKSt14_List_iteratorIiEdeEv> 0x0804870d <+64>:mov (%eax),%ea X 0x0804870f <+66>:add%eax,-0xc (%EBP) 0x08048712 <+69>:lea-0x10 (%EBP),%eax 0x08048715 <+72> ;: Movl $0x0,0x8 (%ESP) 0x0804871d <+80>:lea-0x18 (%EBP),%edx 0x08048720 <+83>:mov%edx,0x4 (%ESP) 0x08048724 <+87>:mov%eax, (%ESP) 0x08048727 <+90>:call 0x8048882 <_ZNSt14_List_iteratorIiEppEi> 0x0804872c <+95>:sub $0x4,%esp 0x0804872f <+98>:lea-0x14 (%EBP),%eax 0x08048732 <+101>:mov 0x8 (%EBP),%edx 0x08048735 <+104>:mov%edx,0x4 (%esp) 0x08048739 <+108>:mov%eax, (%ESP) 0x0804873c <+111>:call 0x804884a <_ZNSt4listIiSaIiEE3endEv> 0x08048741 <+116>:sub $0x4,%esp 0x08048744 < ; +119>:lea-0x14 (%EBP),%eax 0x08048747 <+122>:mov%eax,0x4 (%ESP) 0x0804874b <+126>:lea-0x18 (% EBP),%eax 0x0804874e ≪+129>:mov%eax, (%ESP) 0x08048751 <+132>:call 0x804886e <_ZNKSt14_List_iteratorIiEneERKS0_> 0x08 048756 <+137>:test%al,%al 0x08048758 <+139>:jne 0x8048702 <_Z6getSumRSt4listIiSaIiEE+53> 0x080 4875a <+141>:mov-0xc (%EBP),%eax 0x0804875d <+144>:leave 0x0804875e <+145>:ret End of Assemb Ler dump.
You can see the this pointer of the list in the ebp+0x8,iter of the this pointer in ebp-0x18.
At the 0x0804874b command address break point.
Look at the contents of the list:
(GDB) x $ebp +0x80xbffff5a0:0xbffff5b8 (gdb) x/8x 0xbffff5b80xbffff5b8:0 x0804b0080x0804b0f80x08048c600x080485e00xbffff5c8:0x002edff40x000000000x08048c600x00000000 (GDB) x/4x 0x0804b0080x804b008:0x0804b0180xbffff5b80x000000000x00000011 (GDB) x/4x 0x0804b0180x804b018:0 x0804b0280x0804b0080x000000010x00000011 (GDB) x/4x 0x0804b0280x804b028:0x0804b0380x0804b0180x000000020x00000011 ( GDB) x/4x 0x0804b0380x804b038:0x0804b0480x0804b0280x000000030x00000011 (gdb) x/4x 0x0804b0480x804b048:0 x0804b0580x0804b0380x000000040x00000011 (GDB) x/4x 0x0804b0580x804b058:0x0804b0680x0804b0480x000000050x00000011 ( GDB) x/4x 0x0804b0f80x804b0f8:0xbffff5b80x0804b0e80x0000000f0x00020f01 (gdb) x/4x 0x0804b0e80x804b0e8:0 x0804b0f80x0804b0d80x0000000e0x00000011 (GDB) x/4x 0x0804b0d80x804b0d8:0x0804b0e80x0804b0c80x0000000d0x00000011
Take a look at the changes in ITER's content:
Breakpoint 1, 0x0804874b in Getsum (Std::list<int, std::allocator<int> >&) () (GDB) x/4x $ ebp-0x180xbffff580:0x0804b0180xbffff5b80x0804b0080x00000000 (GDB) Ccontinuing.breakpoint 1, 0x0804874b in GetSum (std :: List<int, Std::allocator<int> >& () (GDB) x/4x $ebp -0x180xbffff580:0 x0804b0280xbffff5b80x0804b0180x00000001 (GDB) Ccontinuing.breakpoint 1, 0x0804874b in Getsum (Std::list<int, std:: Allocator<int> >& () (GDB) x/4x $ebp -0x180xbffff580:0x0804b0380xbffff5b80x0804b0280x00000003 (GDB) Ccontinuing.breakpoint 1, 0x0804874b in Getsum (Std::list<int, std::allocator<int> >&) () (GDB) x/4x $ ebp-0x180xbffff580:0x0804b0480xbffff5b80x0804b0380x00000006
You can get list iterator and only one member _m_node, pointing to List per node (except head node).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Coredump Problem Theory Research "Linux x86 edition 7.9 section list related iterator objects