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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:26:44+00:00 2026-05-26T05:26:44+00:00

Based on this discussion, I was wondering if a function scope static variable always

  • 0

Based on this discussion, I was wondering if a function scope static variable always uses memory or if the compiler is allowed to optimize that away. To illustrate the question, assume a function like such:

void f() {
   static const int i = 3;
   int j = i + 1;
   printf("%d", j);
}

The compiler will very likely inline the value of i and probably do the calculation 3 + 1 at compile time, too. Since this is the only place the value of i is used, there is no need for any static memory being allocated. So is the compiler allowed to optimize the static away, or does the standard mandate that any static variable has memory allocated?

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

    So is the compiler allowed to optimize the static away[…] ?

    Yes. According to the Standard:

    1.9 Program Execution

    The semantic descriptions in this International Standard define a
    parameterized nondeterministic abstract machine. This International
    Standard places no requirement on the structure of conforming
    implementations. In particular, they need not copy or emulate the
    structure of the abstract machine. Rather, conforming implementations
    are required to emulate (only) the observable behavior of the abstract
    machine as explained below.5)

    …and the footnote says:

    5) This provision is sometimes called the “as-if” rule, because an
    implementation is free to disregard any requirement of this
    International Standard as long as the result is as if the requirement
    had been obeyed, as far as can be determined from the observable
    behavior of the program. For instance, an actual implementation need
    not evaluate part of an expression if it can deduce that its value is
    not used and that no side effects affecting the observable behavior of
    the program are produced.

    What this all means is that the compiler can do anything it wants to your code so long as the observable behavior is the same. Since you haven’t take the address of the static const, the compiler can optimize the value away to a Constant Integral Expression.

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

Sidebar

Related Questions

I realize this comes at an enormous risk of being branded subjective and discussion-based,
Based on this question it appears that the default template for CheckStyle will allow
This question is based on this thread . The code function man() { man
I am trying to implement macro replacement based on this discussion . Basically it
Based on this question I don't want to litter my ready stuff waiting for
Based on this article , it seems like SO is using Javascript OpenID Selector
Based on this , how would you make ignoring parameters more succint? var m
Based on this question I understand the purpose of the construct in linking C
This question is based on this thread . Is [---] a comment in Git
This question is based on this thread . I am interested in how Git

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.