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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:04:51+00:00 2026-05-22T19:04:51+00:00

Compilers can sometime exploit the fact that some ‘variable’ is a constant for optimization,

  • 0

Compilers can sometime exploit the fact that some ‘variable’ is a constant for optimization, so it’s generally a good idea to use the “const” keyword when you can, but is there a tradeoff?

In short, is there a situation where using “const” might actually make the code slower (even a tiny bit)?

  • 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-22T19:04:52+00:00Added an answer on May 22, 2026 at 7:04 pm

    The const keyword is used only during compile-time. After the code is compiled the variable is just an address in the memory, without any special protection.

    There is some difference, however – global const variables will be placed in the text segment, not the data (if initialized) or bss (if uninitialized). If the text segment is treated differently, for example executed in place from a NOR flash memory (instead of RAM), there might be a difference. Local const variables are placed on the stack together with the regular variables, so there should be no difference.

    Other than that, as bestsss said, some compile time optimizations might be impossible if the variable is a constant. I can’t really think of anything (especially not in pure C), but it is theoretically possible.

    Edit:

    The following code demonstrated the point in the second paragraph:

    const int g = 1;
    int not_const = 1;
    
    void foo(int param)
    {
        int i = 1;
        const int j = 1;
    
        printf("Variable: \t\t0x%08x\n", (int)&i);
        printf("Const varialbe: \t0x%08x\n", (int)&j);
        printf("Parameter: \t\t0x%08x\n", (int)&param);
        printf("Global const: \t\t0x%08x\n", (int)&g);
        printf("Global non-const: \t0x%08x\n", (int)&not_const);
    

    }

    In Visual Studio 2010, the result is as follows (note the big difference between the const and non-const global):

    Variable: 0x002af444
    Const varialbe: 0x002af440
    Parameter: 0x002af43c
    Global const: 0x00a02104
    Global non-const: 0x00a03018

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

Sidebar

Related Questions

I always wonder why compilers can't figure out simple things that are obvious to
Any existing Flash SWF compilers that can understand directives like #IF #ENDIF , etc?
I know that android programmers can exploit legacy c++ libraries but sometimes I read
I've read that the C++ standard allows optimization to a point where it can
Are there compilers for high-level languages (such as C) which can be targeted to
In my experience Meta-templates are really fun (when your compilers are compliant), and can
In C and C++ you can tell the compiler that a number is a
It seems to me that it would work perfectly well to do tail-recursion optimization
It is clear that cross compilers will not be allowed by the Apple App
I'm developing cross-platform project that would support : Four C++ compilers - GCC, MSVC,

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.