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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:28:45+00:00 2026-05-18T07:28:45+00:00

My understanding about static variables declared inside a function is: If no initial value

  • 0

My understanding about static variables declared inside a function is:

  1. If no initial value is specified, the static variable will reside in .bss, otherwise in .data
  2. The memory for statics are allocated along with globals – i.e., well before the execution enters main
    • are these two assumptions correct ?
  3. When the execution hits the function for the first time, statics are initialized to the user specified value (or zero in case no initial value is specified).
  4. …and they retain their values across subsequent invocations of the function

But what if I declare my static variable inside an if block? I assume my third point should be updated to “when the execution hits the line where the static variable is declared, they’re initialized to … “ – am I right ?

Now, what if the if block in which they’re declared is never hit (and the compiler is able to figure this out) – I understand that the variable will never be initialized; but does any memory get allocated for that variable?

I wrote two functions to try to figure out what’s happening:

#include <stdio.h>

void foo()
{
    static foo_var_out;
    if(0){
        static foo_var_in_0;
        printf("%d %d\n", foo_var_in_0);
    } else {
        static foo_var_in_1;
        printf("%d %d\n", foo_var_in_1);
    }   
}

static void bar(int flag)
{
    static bar_var_out;
    if(flag){
        static bar_var_in_0;
        printf("%d %d\n", bar_var_in_0);
    } else {
        static bar_var_in_1;
        printf("%d %d\n", bar_var_in_1);
    }   
}

int main()
{
    foo();
    bar(0);
}

And I took the object dump:

$ gcc -o main main.c
$ objdump -t main | grep var
45:080495c0 l     O .bss    00000004              foo_var_in_1.1779
46:080495c4 l     O .bss    00000004              foo_var_out.1777
47:080495c8 l     O .bss    00000004              bar_var_in_1.1787
48:080495cc l     O .bss    00000004              bar_var_in_0.1786
49:080495d0 l     O .bss    00000004              bar_var_out.1785

From the output it looks like foo_var_in_0 was not created at all (presumably because it is inside an explicit if(0)), whereas bar_var_in_0 was created (as it is possible for the caller to pass a non-zero value – although the only caller is explicitly passing zero).

I guess my question is: is it correct to assume that no memory was allocated for the variable foo_var_in_0 at all? I am asking about this specific case; am I reading the objdump correctly – or should I be doing something more to verify if the variable will take some memory while the program is ran?

In other words, if the line that declares a function level static variable is never hit, is the variable actually declared at all?

If it will not be created at all, is this according to the C standard (less likely), or a compile time optimization and at what level – how do I turn it ON/OFF (in gcc 4.1.1)?

I understand that one int is not a big deal to care about, but I am more interested in how it works; also, what if the variable was a big array of size, say 5000 elements of a 10 byte struct?

  • 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-18T07:28:46+00:00Added an answer on May 18, 2026 at 7:28 am

    is it correct to assume that no memory was allocated for the variable foo_var_in_0 at all?

    No, I don’t think it would be correct to assume that. As far as I know, optimizations like this are not part of the standard.

    If you know for a fact that you compiler does this and you want to assume it, go ahead. If you write anything that needs this to be the case, you might want to write a post-build test to make sure that it happened.

    Probably, what you are seeing is a side-effect of the compiler just pruning out some code that it knew would never run. Meaning, it’s not specifically looking to remove statics, but it did remove an entire branch, so any code in it just got removed as well.

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

Sidebar

Related Questions

I think this question is really about my understanding of Garbage collection and variable
I have some doubt about python's class variables. As my understanding, if I define
In my understanding a singleton object will destroy only when the application is about
Below is my understanding about the log file group. Whenever innodb tables were inserted/updated
There's something I'm not quite understanding about the use of boilerpipe's ArticleExtractor class. Albeit,
i'm new to c++ and having a little problem understanding about c++'s casting. According
I am using .Net3.5. Just wondering my understanding about the insert, update, delete are
I'm having a tricky debugging issue, perhaps due to my lack of understanding about
Im not understanding something about rest web services. When you do get, post etc
If I'm understanding this correctly about garbage collection in .NET CLR, then GC occurs

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.