Like Tree1Likes
  • 1 Post By anduril462

static variable initialization

This is a discussion on static variable initialization within the C Programming forums, part of the General Programming Boards category; Why is it compulsory to initialize static variables during compile time only?...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    static variable initialization

    Why is it compulsory to initialize static variables during compile time only?

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Are you trying to get homework answers from us?

    Announcements - General Programming Boards

    Your textbook will probably have the answers you're looking for if you would only read it.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    It doesn't, or isn't, you don't... um that question makes no sense at all!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Static local variables have static "storage duration". That means, like global variables, they exist for the entire life of the program. In order for this to be possible, the memory used for them must not be part of the heap (malloc/free stuff) or stack (local, automatic funciton variables). They must exist, in a sense, outside of any function. Yes, the name of that variable is visible within a function, but the memory for that variable is "outside" of that function, i.e. not on the stack. An initialization provides the initial value at the time the variable is defined. Since it must be defined outside a function, and no executable code can exist outside of a function, no instructions can be generated to set that value during run time. Besides, there is no need to initialize it during run time. The variable exists before the program starts, and you know it's initial value, so have that value already ready to go when the program is run. No need to burned the startup time of your program with initializing such variables. Thus the value is set at compile time.
    Saurabh Mehta likes this.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @christop I am just trying to clear the concepts which i dont find by searching on internet.So plz dont make fun of people who want to learn something ...

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @anduril462 thankyou sir as you mentioned they are stored at different location other then heap or stack.So is there any specific name for that memory or is it any free memory im RAM?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    The name it goes by and exactly how it's implemented, may vary from system to system, but generally, it would be called something like "global", or "data". It's location is RAM is generally irrelevant, since modern systems (excluding some embedded ones) use virtual memory mapping. Check out this link for an overview: Anatomy of a Program in Memory : Gustavo Duarte. Google "linux process memory layout" for more examples and info of how Linux does it. Not sure how different Windows is, but you can Google the equivalent.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by Saurabh Mehta View Post
    @christop I am just trying to clear the concepts which i dont find by searching on internet.So plz dont make fun of people who want to learn something ...
    He's not making fun of you, he's trying to ensure you are actually attempting to learn, not just get some free answers from a forum that you don't really understand. In his defense, the question you asked in this thread, as well as the ones in the following threads (all of your threads to date):



    all sound very much like homework questions.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @anduril462 I tried to get the explanations of the previous threads over the internet but was not satisfied..So I chose this forum..and thankfully my concepts are getting cleared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static member initialization with class template
    By threahdead in forum C++ Programming
    Replies: 6
    Last Post: 04-28-2011, 01:16 AM
  2. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  3. Why initialization of static member is a must?
    By meili100 in forum C++ Programming
    Replies: 9
    Last Post: 04-11-2008, 06:22 PM
  4. Replies: 6
    Last Post: 12-13-2007, 08:20 PM
  5. Order of initialization of static variables
    By Sang-drax in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2004, 05:31 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21