In c, when a variable is defined static within a function, the value of the variable is retained all the time. So probably, it's stored in the bss or data section.
However, in Eclipse CDT debugging, when trying to inspect this variable, typing the variable name in the expression pane only display the variable when the stack frame is in the function, like local variable.
Is it a bug in eclipse ?
Edit:
In IAR Embedded workbench, static variables in functions can be watched outside the function in debugging, so it's possible.
While the variable is, in fact, stored in the heap... it remains that the scope of the variable is local to the function. It's value has no meaning outside of that function and it should never change when outside of the function... unless you're passing pointers to it around, in which case you can view it in any function that has access to that pointer.
So... no... it's not a bug.
You need to qualify the static variable with the function in order to determine the scope, e.g. foo::myvar if myvar is defined in function foo.
Related
If static local variable also stored in the data segment, why can't values are not persist for variable which is used in two different functions. example like this.
void func()
{
static int i=0;
i++;
}
void func1()
{
i++; // here i is stored in the data segment,
// then the scope should be available for entire program
}
Why the value 'i' is only accessible to block scope if is stored in data segment? it might be a silly question but I am trying to understand to concept. Please help me to understand concept. Thanks in advance.
You need to differentiate between the scope and the lifetime of a variable.
In simple words:
"scope" means the region of your source code where the variable is known to the compiler. If a variable is (by the rules) not visible to the compiler, it will refuse to compile accesses to it.
"lifetime" means the time beginning with the allocation of memory for the variable until the moment the memory is assigned to another variable or released. A static variable lives as long as the program runs. A non-static variable lives just as long as its scope is in control.
However, just because both scope and lifetime of a variable are "finished", that does not mean that the memory disappears. The physical cells are still there, and they keep their last contents. That's why you can program functions that return a pointer to some local variable, and retrieve that variables contents after both the scope and the lifetime of the variable are gone. This is a fine example of a beginner's confusing issue.
Consider a compiler for an embedded processor like the 8051. Granted, a quite old and simple machine, but a good example. This compiler will commonly put local variables in its data segment. But to use the limited memory space (128 bytes in total, including working registers and stack) the same memory locations are re-used for variables with non-overlapping lifetimes. Eventhough, you could access any memory from all of the program.
Now, language lawyers, start picking on me. ;-)
A variable in C consists of two things:
A name, called an identifier. An identifier has a scope, which is a region of the program source code in which it is visible (may be used).
A region of storage (memory), called an object. An object has a lifetime, which is a portion of program execution during which memory is reserved for it. This is also called storage duration.
For a variable declared inside a function, its identifier has block scope, and the identifier is visible only from its declaration to the } that closes the innermost block it is in. (A block is a list of statements and declarations inside { and }.)
Inside a function, declaring a variable with static makes its object have static storage duration, causing it to exist for all of program execution, but it does not change the scope of its identifier. The object exists throughout program execution, but the identifier is visible only inside the function.
When another function is called, the object still exists (and it can be used if the function has its address, perhaps because it has been passed as a parameter). However, the identifier for the variable is not known inside the source code of other functions, so they cannot use the identifier.
I have tried something like this :
int globalvar=10;
void print ()
{
printf("%d \n",globalvar);
}
int main(){
int globalvar=5;
printf("%d \n",globalvar);
print ();
while (1);
return 0;
}
and then the output went like this
5
10
I have concluded that C has created a LOCAL instance of "globalvar" inside main and it's initialized with the value 10 which is printed out by printf.
I am okay with that point but I have got confused with some new points :
globalvar which is initialized before the main is supposed to have the scope allover the program with its same memory location in DS , what does it happen in that case where there's a variable with its same name defined locally in a function ( ex: main ), ?
second question is related to the linker : How does the linker deal with these two variables of the same names so it executes in the way it executed?
PS : After downvoting the question , I want to say that I really searched the issue and I didn't find a similar question and I thought that asking a question like this will be useful to others.
I wish I am not misunderstod
I have concluded that C has created a LOCAL instance of "globalvar" inside main and it's initialized with the value 10 which is printed out by printf.
No, you created a local variable called globalvar initialized to 5. You then use it in printf. print() has no idea at all about that variable, and uses the global one set to 10.
what does it happen in that case where there's a variable with its same name defined locally in a function ( ex: main ), ?
Local variables take precedence. If you want to use the global variable, you should name your local variable something different so it does not conflict.
This is all within the same file, so the linker does not deal with the interplay of these two variables at all.
First the printf in main is called. It outputs 5 - the value of the local variable defined in main.
Then print is called, and it calls printf to output the value of global variable.
Linker has nothing to worry about - it does not deal with local variables at all, they cannot be accessed by name from outside of the function they are declared in, so the name of a local variable normally is not even visible to the linker.
Your local variable overrides your global variable inside a function. And scope of local variable will end as soon the scope of your function over. So if you call another function which don't have a local variable in same name, you can see the global variable value.
Linker don't have any role in this feature. This feature is managed by the scope of the variable in a program.
There might be another question like this on stack but I am not completely sure. So on to my question. My professor told everyone, "NEVER USE GLOBAL VARIABLES". But she said that static variables are allowed as long as you give a good enough reason. So my question is, under her criteria, is a static variable declared at the global level ok?
Unfortunately static has two meanings in C. When applied to a global variable, it means that the visibility of this symbol is in the file scope. When applied to a local variable, it means that this variable retains its value between calls (i.e., it's not really a local variable). Your professor is referring to the latter, not the former, when she says they are allowed.
There are times when global variables are useful. Consider stderr: it would be a pain to have to define it in every file you needed to use it in; it is sensibly defined as a global variable.
There are times when it is sensible to store a variable at file scope without external linkage (which you do using static as part of the definition of the variable, outside the scope of any function). For example, if you have a suite of functions which need to share some state but the API does not pass a handle back to the calling code (so there isn't an analogue of open and close — or create and destroy), then one or more static variables at file scope make sense.
I know what is the purpose of using static variables in an object oriented language, still, I don't understand what is the meaning of using the "static" keyword in C.
Can someone explain it to me?
On a function or global variable, static makes the function or global variable local to that file; other files cannot access that function or global variable by that name (but they can access it if you give a pointer to it away).
On a local variable, it makes it act as if it was a global variable, but is only accessible within that function (unless, again, you give a pointer to it away).
The value that a static variable has upon leaving a function is the same value that variable will have the next time the function is called.
A static function can be called only from within the same file that the function appears.
gcc 4.4.2 c89
I am have been re-engineering some one else's source code.
In a function someone has declared some static variables, but doesn't seem to serve any purpose of having them static. I am just wondering if my comment below would be accurate?
static char tempstr[64];
For my understanding when declaring static variables inside a function it will retain is scope so acts like a global variable.
Also, if the static variable is declared in global scope, then its scope is limited to the file only.
Many thanks for any suggestions,
If I understand your interpretation, it is accurate.
Inside a function static means "allocate data segment memory so the value persists between function calls and so that all function instances (think, recursion or threads) share the same actual storage."
This matters a lot if a previous value is used in a later function call or if a reference is leaked out of the function by an external call or by returning a pointer.
Yes, your interpretation is correct. Except that be careful with what you call "acts like a global". It acts like a global only in the sense that it retains values between calls, but it's not globally visible, but still only in the function that declared it.
Also see this question.
It doesn't make it a global. Its still a local variable it just retains its value during successive calls.
That's correct. One more aspect to keep in mind is that if your code is multi-threaded, all the threads will share the same copy of the variable, so it is technically possible that the original designer used that variable as a cross-thread communication mechanism. I don't advocate that technique, but I can't rule it out without more information.