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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:22:31+00:00 2026-05-15T21:22:31+00:00

I am a newbie to C++ and facing a problem. I read in a

  • 0

I am a newbie to C++ and facing a problem.

I read in a book that memory is allotted to a static variable, once the object is created of that class. Now, what if I make this static variable global ? When would be it initialized in that case ?

Plus, I have also read in some articles that static variables are allotted on heap and they dont rely on the construction of objects…is this true ? If yes, please explain the memory initialization steps to me, I need help.

Thanks a lot !

  • 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-15T21:22:31+00:00Added an answer on May 15, 2026 at 9:22 pm

    First: STOP THINKING ABOUT GLOBAL VARIABLES IN C AND C++, or you will perpetuate your state of confusion. The issue is more complex than in, e.g., Python or Pascal, and therefore you can’t just use a single word for the concept(s).

    Second, there is no “heap” or “stack” — these are operating system and CPU details, and have nothing to do with the abstract C++ language specification.

    Now, variables have 1) scope, 2) linkage and 3) storage class. The static keyword is used to affect the all three aspects, depending on where you use it.

    Scope: where the variable is declared. If within a function, it is a function-scope, if outside of a function, it is file-scope (what you probably refer to as “global variable”).

    Linkage: whether the variable is accessible from other compilation units (relevant when your program consists of multiple source files).

    Storage class:

    Static variables are allocated in implementation-defined manner upon program startup, and they live until the program ends. They cannot be “freed” or “reallocated”. (a typical implementation are BSS and DATA segments as others have mentioned).

    Automatic variables exist only at function scope, they are allocated (and possibly initialized) on function entry (usually on the CPU’s stack) and deallocated on function exit.

    Dynamic storage class is what you probably refer to “the heap”. Those are variables whose storage is directly manipulated through malloc/free or new/delete. Storage for static variables is managed in a VERY different way than dynamic storage, and the two kinds of storage are fundamentally incompatible.

    Example:

    ===
    // ALL file-scope variables have static storage class
    int global1;        // file-scope, external linkage
    static int global2; // file-scope, internal linkage
    
    void f()
    {
      static int x;     // static storage class, function-scope
      int y;            // automatic storage class, function-scope
    
      free(&x);         // ILLEGAL, WILL CRASH!
      free(&y);         // DITTO!
      free(&global1);   // DITTO!
    }
    ===
    

    Now, all variables with static storage class (global1, global2 and x) are allocated and initialized before program startup. If you do not specify initial values, they are default-initialized in UNSPECIFIED ORDER. (For primitive types, this just means filling with zeros).

    Static variables are deallocated only at program exit. This means, that ‘x’ in function f will be initialized ONLY ONCE (at program startup) and that it will RETAIN ITS VALUE between invocations of the function (as opposed to y which will be allocated on each function entry and deallocated at each function exit, thus also its value destroyed). Note that using static variables within functions is very incompatible with multithreading and reentrancy, unles you know very well what you’re doing.

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

Sidebar

Related Questions

Newbie question: Python 2.6, Ubuntu 10.04, I can import both pycurl and curl ,
Newbie question … sorry ;-) I have to write and to integrate a new
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
Newbie Common Lisp question here. Is there a way to reset the state of
Newbie with databases, I would like some advise please.. I have agencies who can
newbie to iPhone game development and Box2D here. I'm developing a game in which
newbie here. This is an iPhone utility project. First things first. I have a
LaTeX newbie here. I need to set background color for all my \subsection titles.
php newbie here..I need some PHP help ideas/examples on how to import data from
Very newbie question. I want to overwrite the Main in my WPF app so

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.