GCC emits extra code for boost::shared_ptr dereference

Posted by Checkers on Stack Overflow See other posts from Stack Overflow or by Checkers
Published on 2010-03-21T23:01:45Z Indexed on 2010/03/21 23:11 UTC
Read the original article Hit count: 404

Filed under:
|
|
|
|

I have the following code:

#include <boost/shared_ptr.hpp>

struct Foo { int a; };
static int A;

void
func_shared(const boost::shared_ptr<Foo> &foo) {
    A = foo->a;
}

void
func_raw(Foo * const foo) {
    A = foo->a;
}

I thought the compiler would create identical code, but for shared_ptr version an extra seemingly redundant instruction is emitted.

Disassembly of section .text:

00000000 <func_raw(Foo*)>:
   0:   55                      push   ebp
   1:   89 e5                   mov    ebp,esp
   3:   8b 45 08                mov    eax,DWORD PTR [ebp+8]
   6:   5d                      pop    ebp
   7:   8b 00                   mov    eax,DWORD PTR [eax]
   9:   a3 00 00 00 00          mov    ds:0x0,eax
   e:   c3                      ret
   f:   90                      nop

00000010 <func_shared(boost::shared_ptr<Foo> const&)>:
  10:   55                      push   ebp
  11:   89 e5                   mov    ebp,esp
  13:   8b 45 08                mov    eax,DWORD PTR [ebp+8]
  16:   5d                      pop    ebp
  17:   8b 00                   mov    eax,DWORD PTR [eax]
  19:   8b 00                   mov    eax,DWORD PTR [eax]
  1b:   a3 00 00 00 00          mov    ds:0x0,eax
  20:   c3                      ret

I'm just curious, is this necessary, or it is just an optimizer's shortcoming?

Compiling with g++ 4.1.2, -O3 -NDEBUG.

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost