tags:

views:

601

answers:

2

Some people are saying " static variable store its value in HEAP ", and others saying " static variable store store its value in DATA segment". I am totally confused with these conflict answers.

Where exactly static variable stores?. I am expecting an answer with standard reference ( text books, or good author tutorial).

Static variables two types 1) with in function static variable 2) global( outside function ) static variable. Is there any storage location difference between these two type of variables?

A: 

Stack memory is allocated when you launch your application and always remains the same size during the execution of the application. It's not stored in the DATA segment, DATA segment is for things like constant values used in the application (such as string literals).

reko_t
+2  A: 

The 'stack variables' are usually stored on 'the stack', which is separate from the text, data, bss and heap sections of your program.

The second half of your question is about 'static' variables, which are different from stack variables - indeed, static variables do not live on the stack at all. Classically, static variables would all be in the data or bss sections of your program. With modern compilers, if the data is const-qualified, then the data may be stored in the text section of your program, which has a variety of benefits (including enforced non-modifiability).

The C standard does not dictate that there is a stack, nor a bss section. It just requires storage space to be available for variables with appropriate durations.

Jonathan Leffler