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 8394021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:57:30+00:00 2026-06-09T19:57:30+00:00

I have a simple function that takes two variables by reference: void foo(int*& it2,

  • 0

I have a simple function that takes two variables by reference:

void foo(int*& it2,
         bit_reader<big_endian_tag>& reader2)
{
    for(/* ... */)
    {
        *it2++ = boo(reader2.next());
        // it2++ => 0x14001d890 add qword ptr [r12], 0x4
    }
}

The problem here is that for it2 and reader2 the optimizer makes the computer write to memory instead of registers during the loop.

However, the following code puts the variables properly into registers during the loop, but has an extra overhead in the form of unnecessary copies, before and after the loop:

void foo2(int*& it2,
         bit_reader<big_endian_tag>& reader2)
{
    auto reader = reader2;
    auto it     = it2;

    for(/* ... */)
    {
        *it++ = boo(reader.next());
        // it++ => 0x14001d890 add r15, 0x4
    }

    reader2 = reader;
    it2 = it;
}

e.g.

How can I make the first example generate the same code as the second example but without the extra copies?

  • 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-06-09T19:57:31+00:00Added an answer on June 9, 2026 at 7:57 pm

    The problem is that the compiler cannot prove it2 does not change within the function. (Well, it could, but that’s vastly beyond the intended capabilities of a normal C++ compiler.)

    How does it know boo(reader2.next()); doesn’t change the value? Consider:

    int* i = 0;
    
    struct foo
    {
        int myInt;
        int blah() { i = &myInt; return 5; }
    };
    
    void bar(int*& ptr, const foo& f)
    {
        *ptr = f.blah(); // changes value of ptr!
    }
    
    int otherInt;
    i = &otherInt;
    
    bar(i, foo());
    

    This does not assign anything to otherInt, whereas after your transformation it would:

    void bar(int*& ptr, const foo& f)
    {
        int* ptrCopy = ptr;
        *ptrCopy = f.blah(); // changes ptr, but not ptrCopy
    }
    

    So unless the compiler can prove the behavior is the same, it cannot make the optimization.

    C99 solves this problem with the restrict keyword, but C++ has no equivalent. There are extensions that exist in most C++ compilers though, such as __restrict__ or __restrict.

    To do it in standard C++, you just have to be explicit and make the copy yourself

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

Sidebar

Related Questions

I have a simple javascript function that takes two variables. I need to pass
I have a very simple function that takes a list of comma separated (x,y)
I have a simple function that shows loading spinner while fetching data (usually takes
I wrote the following simple function that takes two parameters mostly coming from another
I have to write a library which contains a function that takes two strings
I have this very simple br2nl function that I use to take a string
I have a simple function that looks at an incoming mySQL data type and
I have a simple function that works in chrome, but in Internet explorer (9)
I have a simple function that returns an NSString after decoding it. I use
I have a simple function that I'd like to test (perhaps mostly to appease

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.