Why is it compulsory to initialize static variables during compile time only?
Why is it compulsory to initialize static variables during compile time only?
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.
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"
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.
@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 ...
@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?
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.
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.
@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