how does a static variable not get reassigned when inside the function
Asked Answered
L

3

6

I have a question regarding the answer in this question but I can not comment on it because I have less than 50 rep.

I was wondering in the answer foo() is being called multiple times and the static variable is being assigned the same number of times. So why is it that the static variable is not reassigned to 10 each time?

Lappet answered 30/3, 2017 at 5:42 Comment(1)
You can think of the initializer for a static variable being processed during application startup (before main is entered)Smith
P
2

Actually static variables can get reassigned. But can't be redefined.

Once a static variable defined it can't get redefined throughout the life time of program. But we can change the value.

Poplin answered 30/3, 2017 at 5:47 Comment(2)
Oh okay my bad I didn't realize it was being redefined. I see it now thank you!Lappet
so, non-static variables can be redefined? That's a bit of overstatement and can be misleading....Porche
P
3

You've got the shorter answer, but let me expand a bit on that.

Any object, has a storage duration. The storage duration determines the "lifetime" of the object (or variable).

Static storage is one of the storage durations, marked by keyword static. Now, to elaborate on lifetime, let us check the relevant parts of the C11 standard, chapter §6.2.4.

From paragraph 2,

The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address and retains its last-stored value throughout its lifetime. [....]

So, the last stored value is retained throughout the lifetime.

Now, for the objects with static storage duration, paragraph 3,

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

Now, referring to your question, the statement you saw is the initialization and as per the rule specified, it happens only once (prior to program startup), so the initialization is not repeated over for multiple function calls. The variable retains the last-stored value.

Porche answered 30/3, 2017 at 6:6 Comment(0)
P
2

Actually static variables can get reassigned. But can't be redefined.

Once a static variable defined it can't get redefined throughout the life time of program. But we can change the value.

Poplin answered 30/3, 2017 at 5:47 Comment(2)
Oh okay my bad I didn't realize it was being redefined. I see it now thank you!Lappet
so, non-static variables can be redefined? That's a bit of overstatement and can be misleading....Porche
L
0

When you define a static or global variable it goes in data segment of memory model and it occupies that allocation for the life of the program, of course scope is associated with each variable where it is defined. So when you enter the function again, the variable exists and it also remembers the last content. So ideally you cannot redefine a variable, if you bound it with condition than scope will protect it and hence it is a new variable.

Literality answered 30/3, 2017 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.