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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:15:27+00:00 2026-05-23T05:15:27+00:00

I know that it improves readability and makes the program less error-prone, but how

  • 0

I know that it improves readability and makes the program less error-prone, but how much does it improve the performance?

And on a side note, what’s the major difference between a reference and a const pointer? I would assume they’re stored in the memory differently, but how so?

  • 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-23T05:15:28+00:00Added an answer on May 23, 2026 at 5:15 am

    [Edit: OK so this question is more subtle than I thought at first.]

    Declaring a pointer-to-const or reference-of-const never helps any compiler to optimize anything. (Although see the Update at the bottom of this answer.)

    The const declaration only indicates how an identifier will be used within the scope of its declaration; it does not say that the underlying object can not change.

    Example:

    int foo(const int *p) {
        int x = *p;
        bar(x);
        x = *p;
        return x;
    }
    

    The compiler cannot assume that *p is not modified by the call to bar(), because p could be (e.g.) a pointer to a global int and bar() might modify it.

    If the compiler knows enough about the caller of foo() and the contents of bar() that it can prove bar() does not modify *p, then it can also perform that proof without the const declaration.

    But this is true in general. Because const only has an effect within the scope of the declaration, the compiler can already see how you are treating the pointer or reference within that scope; it already knows that you are not modifying the underlying object.

    So in short, all const does in this context is prevent you from making mistakes. It does not tell the compiler anything it does not already know, and therefore it is irrelevant for optimization.

    What about functions that call foo()? Like:

    int x = 37;
    foo(&x);
    printf("%d\n", x);
    

    Can the compiler prove that this prints 37, since foo() takes a const int *?

    No. Even though foo() takes a pointer-to-const, it might cast the const-ness away and modify the int. (This is not undefined behavior.) Here again, the compiler cannot make any assumptions in general; and if it knows enough about foo() to make such an optimization, it will know that even without the const.

    The only time const might allow optimizations is cases like this:

    const int x = 37;
    foo(&x);
    printf("%d\n", x);
    

    Here, to modify x through any mechanism whatsoever (e.g., by taking a pointer to it and casting away the const) is to invoke Undefined Behavior. So the compiler is free to assume you do not do that, and it can propagate the constant 37 into the printf(). This sort of optimization is legal for any object you declare const. (In practice, a local variable to which you never take a reference will not benefit, because the compiler can already see whether you modify it within its scope.)

    To answer your “side note” question, (a) a const pointer is a pointer; and (b) a const pointer can equal NULL. You are correct that the internal representation (i.e. an address) is most likely the same.

    [update]

    As Christoph points out in the comments, my answer is incomplete because it does not mention restrict.

    Section 6.7.3.1 (4) of the C99 standard says:

    During each execution of B, let L be any lvalue that has &L based on P. If L is used to
    access the value of the object X that it designates, and X is also modified (by any means),
    then the following requirements apply: T shall not be const-qualified. …

    (Here B is a basic block over which P, a restrict-pointer-to-T, is in scope.)

    So if a C function foo() is declared like this:

    foo(const int * restrict p)
    

    …then the compiler may assume that no modifications to *p occur during the lifetime of p — i.e., during the execution of foo() — because otherwise the Behavior would be Undefined.

    So in principle, combining restrict with a pointer-to-const could enable both of the optimizations that are dismissed above. Do any compilers actually implement such an optimization, I wonder? (GCC 4.5.2, at least, does not.)

    Note that restrict only exists in C, not C++ (not even C++0x), except as a compiler-specific extension.

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

Sidebar

Related Questions

I know that the usage of Microsoft CDN improves the website performance when talks
I know that using continuous integration improves the quality of my code base, and
I know that default cron's behavior is to send normal and error output to
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that IList is the interface and List is the concrete type but
So does implementing Dispose() on the managed objects where would improve Garbage Collector's performance
I know that the asp.net repeater doesnt have a Client side object model, and
We all know that Mathematica is great, but it also often lacks critical functionality.
I have been told that disabling backreferences in perl improves performance (provided you're not
I know that I can do something like $int = (int)99; //(int) has a

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.