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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:03:56+00:00 2026-05-13T10:03:56+00:00

Consider the following: struct Point {double x; double y;}; double complexComputation(const& Point p1, const

  • 0

Consider the following:

struct Point {double x; double y;};

double complexComputation(const& Point p1, const Point& p2)
{
    // p1 and p2 used frequently in computations
}

Do compilers optimize the pass-by-reference into pass-by-copy to prevent frequent dereferencing? In other words convert complexComputation into this:

double complexComputation(const& Point p1, const Point& p2)
{
    double x1 = p1.x; double x2 = p2.x;
    double y1 = p1.y; double y2 = p2.y;
    // x1, x2, y1, y2 stored in registers and used frequently in computations
}

Since Point is a POD, there can be no side effect by making a copy behind the caller’s back, right?

If that’s the case, then I can always just pass POD objects by const reference, no matter how small, and not have to worry about the optimal passing semantics. Right?

EDIT:
I’m interested in the GCC compiler in particular. I guess I might have to write some test code and look at the ASM.

  • 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-13T10:03:56+00:00Added an answer on May 13, 2026 at 10:03 am

    There are 2 issues.

    Firstly, the compiler will not convert pass-by-ref to pass-by-value, especially if complexComputation is not static (i.e. can be used by external objects).

    The reason is API compatibility. To the CPU, there is no such thing as a “reference”. The compiler will convert references to pointers. Parameters are passed on stack or via register, so a code calling complexComputation will likely be called as (assume double is of length 4 for a moment):

    str x1, [r7, #0x20]
    str y1, [r7, #0x24]
    str x2, [r7, #0x50]
    str y2, [r7, #0x54]
    push r7, #0x20     ; push address of p1 onto the stack
    push r7, #0x50     ; push address of p2 onto the stack
    call complexComputation
    

    Only 8 bytes are pushed onto the stack.

    Pass by copy, on the other hand, will push the whole struct onto the stack, so the assembly code will look like

    push x1    ; push a copy of p1.x onto the stack
    push y1    ; push a copy of p1.y onto the stack
    push x2    ; push a copy of p2.x onto the stack
    push y2    ; push a copy of p2.y onto the stack
    call complexComputation
    

    Note that this time 16 bytes are pushed onto the stack, and the content are the numbers, not pointers. If the complexComputation changes its parameter passing semantics, the input will become garbage and your program may crash.


    On the other hand, the optimization

    double complexComputation(const Point& p1, const Point& p2) {
        double x1 = p1.x; double x2 = p2.x;
        double y1 = p1.y; double y2 = p2.y;
        // x1, x2, y1, y2 stored in registers and used frequently in computations
    }
    

    can be easily done, since the compiler can recognize what variables are used very often and
    store them into reserved registers (e.g. r4 ~ r13 in the ARM architecture, and many of the sXX/dXX registers) for faster access.


    After all, if you want to know if a compiler has done something, you can always disassemble the resulting objects and compare.

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

Sidebar

Ask A Question

Stats

  • Questions 311k
  • Answers 311k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do that using the SQL's Update statement: UPDATE… May 13, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer You have 3 issues happening. The javascript include for jQuery… May 13, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer For this to work the policy class needs to inherit… May 13, 2026 at 10:23 pm

Related Questions

I frequently read that struct s should be immutable - aren't they by definition?
Several comments on a recent answer of mine, What other useful casts can be
Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)
We have a class whose semantic behaviour is like the following :- struct Sample
For debugging purposes, I find it useful to display the contents of data structures.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.