What is a static local variable in C? I'm supposed to write short essay answers in including tracing the output of code, or code writing.
Computer Science Quesetion?
A static local variable is a variable defined inside a function that does not get deallocated when the function ends (like normal, stack-allocated function local variables). Thus from one function call to the next it maintains the same value at the start of a function call that it had at the end of the previous function call.
It is allocated on the applications startup stack frame, the same way that static global variables are allocated. However, it does not have scope (visibility and accessibility) outside the function in which it is defined.
Here is a simple example:
--------------
void doSomething(int dumpFlag) {
static int callCount = 0;
callCount++;
/* do something here */
if (dumpFlag) {
printf("doSomething() has been called %d times.\n", callCount);
}
--------------
--------------
ASIDE TO Metaferia:
Your example demonstrates a static data member in a C++ class, not a static local variable in a C function.
Reply:class Request
{
private:
static int count;
string url;
public:
Request() { count++; }
string Url(string value) { url = value; return url; }
int Count() const { return count; }
};
int Request::count = 0;
anemone
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment