Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 677859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:05:08+00:00 2026-05-14T01:05:08+00:00

I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:05:08+00:00Added an answer on May 14, 2026 at 1:05 am

    It’s not a ‘redundant’ instruction.

    The relevant section of the first code snippet is equivalent to:
    *p

    Whilst in the second it’s equivalent to:
    **p

    Due to shared_ptr’s internals there is a second level of indirection. This is not something the optimizer can ‘fix’.

    At any rate, the difference is negligible.

    EDIT:

    Whoops! My apologies, I misread your code.

    You are passing the shared_ptr BY REFERENCE in your code. That will pass it “by pointer” at the ASM level.

    So you’re passing a pointer to a shared_ptr, and that shared_ptr contains a pointer to your object.

    Hence the two levels of indirection.

    Sorry for the confusion. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
I have the following code: #include <iostream> #include boost/unordered_map.hpp using namespace std; using namespace
I have a question about the following code: #include <iostream> #include <boost/scoped_ptr.hpp> class Interface
I have written the following code #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/filesystem.hpp>
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
The following code: #include <vector> #include <algorithm> struct myStructDim { int nId; int dwHeight;
I have the following bit of legacy C++ code that does not compile: #include
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have the following code: #include <iostream> #include <string> #include <unistd.h> using namespace std;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.