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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:52:29+00:00 2026-06-18T08:52:29+00:00

I know that using the const keyword on function arguments provides better performance, but

  • 0

I know that using the const keyword on function arguments provides better performance, but I always forget to add it. Is the compiler (GCC in this case) smart enough to notice that the variabele never changes during the function, and compile it as if I would have added const explicitly?

  • 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-18T08:52:30+00:00Added an answer on June 18, 2026 at 8:52 am

    Is the compiler (GCC in this case) smart enough to notice that the
    variabele never changes during the function, and compile it as if I
    would have added const explicitly?

    Not necessarily. For example:

    void some_function(int *ptr); // defined in another translation unit
    
    int foo(int a) {
        some_function(&a);
        return a + 1;
    }
    

    The compiler can’t see what some_function does, so it can’t assume that it won’t modify a.

    Link-time optimization could perhaps see what some_function really does and act accordingly, but as far as this answer is concerned I’ll consider only optimization for which the definition of some_function is unavailable.

    int bar(const int a) {
        some_function((int*)&a);
        return a + 1;
    }
    

    The compiler can’t see what some_function does, but it can assume that the value of a does not change anyway. Therefore it can make any optimizations that apply: maybe it can keep a in a callee-saves register across the call to some_function; maybe it computes the return value before making the call instead of after, and zaps a. The program has undefined behavior if some_function modifies a, and so from the compiler’s POV once that happens it doesn’t matter whether it uses the “right” or “wrong” value for a.

    So, by in this example by marking a const you have told the compiler something that it cannot otherwise know — that some_function will not modify *ptr. Or anyway that if it does modify it, then you don’t care what your program’s behavior is.

    int baz(int a) {
        some_function(NULL);
        return a + 1;
    }
    

    Here the compiler can see all relevant code as far as the standard is concerned. It doesn’t know what some_function does, but it does know that it doesn’t have any standard means to access a. So it should make no difference whether a is marked const or not because the compiler knows it doesn’t change anyway.

    Debugger support can complicate this situation, of course — I don’t know how things stand with gcc and gdb, but in theory at least if the compiler wants to support you breaking in with the debugger and modifying a manually then it might not treat it as unmodifiable. The same applies to the possibility that some_function uses platform-specific functionality to walk up the stack and mess with a. Platforms don’t have to provide such functionality, but if they do then it conflicts with optimization.

    I’ve seen an old version of gcc (3.x, can’t remember x) that failed to make certain optimizations where I failed to make a local int variable const, but in my case gcc 4 did make the optimization. Anyway, the case I’m thinking of wasn’t a parameter, it was an automatic variable initialized with a constant value.

    There’s nothing special about a being a parameter in any of what I’ve said — it could just as well be any automatic variable defined in the function. Mind you, the only way to for a parameter to get the effect of initialization with a constant value is to call the function with a constant value, and for the compiler to observe the value for that call. This tends to happen only when the function is inlined. So inlined calls to functions can have additional optimizations applied to them that the “out-of-line” function body isn’t eligible for.

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

Sidebar

Related Questions

I know that C# has the using keyword, but using disposes of the object
I know that using Dim currUser As String = Request.ServerVariables(LOGON_USER) returns the Domain\Username, but
I know that this has already been asked here but the answer (using a
I know that using the following codes will make each column have unique values,
I know that using friends_work_history permission, we can access friend's work history. Now, I
In PHP, I know that using parameterized queries is the best way to prevent
Yes, I know that downcast using dynamic_cast can't compile if the Base is not
I know that this can be easily done by using if(i%5 == 0 OR
I know that in order to download a cookie associated with an account using
I know that it's possible to implicitly provide asynchronous interaction using: Asynchronous Delegates Asynchronous

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.