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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:07:18+00:00 2026-06-14T11:07:18+00:00

Example available at ideone.com : int passByConstPointerConst(MyStruct const * const myStruct) int passByValueConst (MyStruct

  • 0

Example available at ideone.com:

int passByConstPointerConst(MyStruct const * const myStruct)
int passByValueConst       (MyStruct const         myStruct)

Would you expect a compiler to optimize the two functions above such that neither one would actually copy the contents of the passed MyStruct?

I do understand that many optimization questions are specific to individual compilers and optimization settings, but I can’t be designing for a single compiler. Instead, I would like to have a general expectation as to whether or not I need to be passing pointers to avoid copying. It just seems like using const and allowing the compiler to handle the optimization (after I configure it) should be a better choice and would result in more legible and less error prone code.

In the case of the example at ideone.com, the compiler clearly is still copying the data to a new location.

  • 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-14T11:07:19+00:00Added an answer on June 14, 2026 at 11:07 am

    In the first case (passing a const pointer to const) no copying occurs.

    In the second case, copying does occur and I would not expect that to be optimized out if for no other reason because the address of the object is taken and then passed through an ellipsis into a function and from the point of view of the compiler, who knows what the function does with that pointer?

    More generally speaking, I don’t think changing call-by-value into call-by-reference is something compilers do. If you want copy by reference, implement it yourself.

    Is it theoretically possible that a compiler could detect that it could just convert the function to be pass-by-reference? Yes; nothing in the C standard says it cannot..

    Why are you worrying about this? If you are concerned about performance, has profiling shown copy-by-value to be a significant bottleneck in your software?

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

Sidebar

Related Questions

Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
We need to set the domain as .example.com to make it available for *.example.com
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
Is there any help, resources, example or a tutorial available for putting fragments inside
Example: an Employee table with an optional DateOfBirth field can be normalized into two
Is a tutorial or code example available which embeds a podcast from a URL
I'm looking for a simple example to connect two web parts to eachother. My
Consider the following example (also available here: http://jsfiddle.net/hq8Fg/1/ ). It works fine in IE9,
I am trying to build the mvc-showcase example available here link . But i
I was told there is a qt oscilloscope example available online, not sure if

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.