4

As per the reference, many of them are says like it is stored in data segment. Which means if I declared the same static variable name in two more functions, the data segment contains more than a declaration of same variable. If it is like this, the collision occurs or not?.

So, where the static variables are stored. I think it is stored in text segment. So, only at each iteration of function, the value in the static variable remains same. If it is in data segment, the collision occurs. If it is in stack, the scope of the variable is extend upto the life time of the function, so it does not able to main the value stored in the variable. Is it right?

mohangraj
  • 9,842
  • 19
  • 59
  • 94
  • text segment stores program code. variables cannot be stored in text segment because they are not a program code. (cap.obvious) – mangusta Aug 11 '15 at 05:46
  • 3
    *Global* static variables, or *local* static variables? And just so you know, this is up to the implementation (the compiler) and the C specification doesn't say anything about this (or things like "segments"). – Some programmer dude Aug 11 '15 at 05:51
  • 2
    The confusion you have stems from you belief that if you use the same name in two different functions, then there will be a clash of names. That is not the case. There is no such clash. Two distinct variables are created. Get past that misunderstanding and your question dissolves. – David Heffernan Aug 11 '15 at 05:54
  • The compiler adds a prefix to the name of each global static variable, thus making it unique. The prefix is typically derived from the name of the file in which the variable is declared (that is why you might get a linkage error when declaring such variable in a header file - if the header file is included in more than one source file, then the same global variable will be declared more than once). – barak manos Aug 11 '15 at 06:10
  • Avoiding the should you be asking these questions or not. Initialized static variables are stored in the .data segment for most compilers. If you leave the variable uninitialized (or initialized to 0) in either global or function scope it will be placed in the .bss segment and set to 0 on runtime startup. – D M Lowe Aug 03 '21 at 18:28

4 Answers4

2

Static variables are stored in Datasegment in the memory. Thier life time is till the program terminates. but their scope is local to the function in which they are declared. if declared global then their scope is limited to the file in which they are declared.

  • If variables are stored in data segment, then the variables are accessed from anywhere from the program like global variable. Isn't or not? Then how, the static variables are placed in data segment as well as its scope is with the function. Is it possible? – mohangraj Aug 11 '15 at 06:19
  • 1
    @mohan Why do you care about segments? It sounds like your mental models of how this all works are way off. You are thinking about this at the wrong level. Forget about segments at least for now. After all, the standard makes no reference to them. Concentrate on understanding the language rather than one specific implementation. – David Heffernan Aug 11 '15 at 06:39
  • @DavidHeffernan It is not like that. Just I want to know how the static variables are internally stored like other variable. For Ex: If I want to take a session about this, I must know about a to z of the static variable. On there, I am not able to tell these type of answer. And we don't says like there is no reference for standard implementation. The way we are searching for the answer is wrong. But, somewhere the answer will be available. We want to find until we get the answer. Anyway thankyou for you help.... – mohangraj Aug 11 '15 at 07:00
  • You aren't going about this the right way. Knowing where they are stored is not helpful. What about implementations on machines that don't have segments organised the same way as your machine? Understand the language at a high level first. – David Heffernan Aug 11 '15 at 07:01
  • 1
    @mohan : my friend static variables are stored in data segment and according to you it is accessible from anywhere within the program like global variables. I understand your point but let me make it clear to you that scope of static variable is within the module where it is used but it's lifetime is throughout the life of program. – Rishabh Dwivedi Aug 12 '15 at 06:48
0

In Initialized data segment, Generally also called the Data Segment. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.

WedaPashi
  • 3,561
  • 26
  • 42
0

The storage location of a variable depends on the life time of the variable. The life time of a static variable is that of the entire program. So static variables along with global variables are stored in the data segment of your memory.

Deepu
  • 7,592
  • 4
  • 25
  • 47
0

Static variables are stored in the data segment of the code. Actually, memory is divided into 3 main parts:

  • Text, Data and Stack/Heap

Text segment only stores the actual executable after compilation of the code. So there is no way static variables are stored in the text segment.

Stack and Heap contain the dynamic data, ie., the variables declared for a scope (local variables) in stack), global/extern/unallocated types in heap (memory allocated through malloc, calloc etc also) and call of every function creates a new stack.
this is one of the reason that many recursive codes cause segmentation fault due to stack overflow

Now, the variables with static linkage (static variables, read only data and uninitialized data has nowhere else to go other than Data Segment of the memory. Now there can be local static variables and global static variables. C always prefers local variables to global with same names, so conflict in name can't occur in 'c' unless variables with same name are declared in same scope.

vish4071
  • 5,135
  • 4
  • 35
  • 65