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

  • Home
  • SEARCH
  • 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 6755353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:23:22+00:00 2026-05-26T13:23:22+00:00

Passing an object by reference is an easier, faster and safer way to pass

  • 0

Passing an object by reference is an easier, faster and safer way to pass an address to it.
But for most compilers, it’s all the same: references are really pointers.

Now what about basic types like int? Passing an address to an int and using it inside a function would be slower than passing it by copy, because the pointer needs to be dereferenced before use.

How do modern compiler handle, this?

int foo(const int & i)
{
   cout << i; // Do whatever read-only with i.
}

May I trust them to compile this into this?

int foo(const int i)
{
   cout << i;
}

By the way, in some cases it could even be faster to pass both i and &i, then use i for reading, and *i for writing.

int foo(const int i, int * ptr_i)
{
   cout << i;    // no dereferencement, therefore faster (?)
   // many more read-only operations with i.
   *ptr_i = 123;
}
  • 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-26T13:23:23+00:00Added an answer on May 26, 2026 at 1:23 pm

    Visual Studio 2010 (Express) does, in the simple cases I’ve tested at least. Anyone to test gcc?

    I’ve tested the following:

    1. Passing only i:

    int vars[] = {1,2,3,12,3,23,1,213,231,1,21,12,213,21321,213,123213,213123};
    
    int ok1(const int i){
        return sqrtl(vars[i]);
    }
    
    int ok2(const int & i){
        return sqrtl(vars[i]);
    }
    
    void main() {
        int i;
        std::cin >> i;
        //i = ok1(i);
        i = ok2(i);
        std::cout << i;
    }
    

    The ASM:

    i = ok1(i);
    000D1014  mov         ecx,dword ptr [i]  
    000D1017  fild        dword ptr vars (0D3018h)[ecx*4]  
    000D101E  call        _CIsqrt (0D1830h)  
    000D1023  call        _ftol2_sse (0D1840h) 
    
    i = ok2(i);
    013A1014  mov         ecx,dword ptr [i]  
    013A1017  fild        dword ptr vars (13A3018h)[ecx*4]  
    013A101E  call        _CIsqrt (13A1830h)  
    013A1023  call        _ftol2_sse (13A1840h)
    

    Well, the ASMs are identical, no doubt the optimization was performed.

    2. Passing i and &i:

    Let’s consider @newacct ‘s anser here.

    int vars[] = {1,2,3,12,3,23,1,213,231,1,21,12,213,21321,213,123213,213123};
    
    int ok1(const int i, int * pi) {
        *pi = 2;
        return sqrtl(vars[i]);
    }
    
    int ok2(const int & i, int * pi) {
        *pi = 2;
        return sqrtl(vars[i]);
    }
    
    void main() {
        int i;
        int * pi = &i;
        std::cin >> i;
        i = ok1(i, pi);
        //i = ok2(i, pi);
        std::cout << i;
    }
    

    The ASM:

    i = ok1(i, pi);
    00891014  mov         ecx,dword ptr [i]
    00891017  fild        dword ptr vars (893018h)[ecx*4] // access vars[i] 
    0089101E  call        _CIsqrt (891830h)  
    00891023  call        _ftol2_sse (891840h)  
    
    i = ok2(i, pi);
    011B1014  fild        dword ptr [vars+8 (11B3020h)]   // access vars[2]
    011B101A  call        _CIsqrt (11B1830h)  
    011B101F  call        _ftol2_sse (11B1840h) 
    

    In ok1 I can’t see it writing 2 into pi. Probably it understands that the memory location will be overwritten by the result of the function anyway, so the writing is useless.

    With ok2, the compiler is as smart-ass as I expected. It understands that i and pi point to the same place, so it uses a hardcoded 2 directly.

    Notes:

    • I’ve compiled twice for both test, once uncommenting only ok1, once uncommenting only ok2. Compiling both at the same time leads to more complex optimizations between the two functions, which end up all inlined and mixed up
    • I’ve added a lookup in the array vars because simple calls to sqrtl were simplified into basic ADD- and MUL-like operations without the actual call
    • Compiled in Release
    • Yielded the expected results, of course
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am passing one parameter/object to the function RefValTest(object oIn) by the value but
Motivation Recently I searched for a way to initialize a complex object without passing
I'm passing an object reference to a Util class. I'm attempting to invoke a
I am having some troubles passing a reference to an object which is of
Possible Duplicates: Why use ref keyword when passing an Object? When to pass ref
Possible Duplicate: Passing an IDisposable object by reference causes an error? Why doesn't C#
If I am passing an object to a method, why should I use the
I am passing an object from one asp.net page to another. I'm encoding the
I have a get method passing in an object of type Store to the
How can I instantiate an anonymous object while passing the propertynames and values as

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.