5 ms·
From the article> "Static Function Variable: In C inside a function, you can declare a static variable. You could consider this as a local variable but I don't
by billforsternz 2y ago
From the article> "Static Function Variable: In C inside a function, you can declare a static variable. You could consider this as a local variable but I don't since it's on the heap, and can be returned and modified outside of the functions. I absolutely avoid this except as a counter whose address I never return."
These variables are not on the heap. They are statically allocated. Their visibility is the only thing that differentiates them from global variables and static variables defined outside of functions.
I think such variables can be useful, if you need a simple way of keeping some persistent state within a function. Of course it's more state you are carrying around, so it's hard to defend in a code review as best practice.
Amusingly, you can modify such variables from outside the function, simply by getting your function to provide the modifying code with a pointer to the variable, eg by returning the variable's address. If you do that though you're probably creating a monster. In contrast I think returning the value of the static variable (which the author casts shade on in the quote above) seems absolutely fine.
Edit: I should have stated that the number one problem with this technique is that it's absolutely not thread safe for obvious reasons.
- ijustlovemath 2y agostatic variable addresses are an extremely important tool in static analysis, for proving certain program invariants hold. In C, a const static often will be able to tell you more about the structure of your code at runtime than a #define macro ever could! though unless you're programming extremely defensively (eg trying to thwart a nation state), I see no reason why you would use them at runtime
- Panzer04 2y agoCould you expand on this? I don't understand the point you're making, or how it's useful.
- whitten 2y agoPerhaps a distinction between static analysis (at compile) and dynamic analysis (at run time) is useful ?
- ijustlovemath 2y agostatic consts in C carry their identity through their (fixed, unchanging) pointer address. Lets say you have a business rules engine, that's only meant to ingest threshold values from a certain module. You want to know if the 3.0 you're using is coming from the correct place in the code, or if there's a programming error. With a define, there's not enough additional information to be able to. With a static const, you can just have a const static array of pointers to the valid threshold constants, and use that in your tests for the rules engine. I work in a highly regulated field, so often this level of paranoia and proof helps us make our case that our product works exactly the way we say it works.
- Panzer04 2y agoSo it's easier to guarantee the identity is correct? That doesn't stop you from using the incorrect identity, though. I'm not quite sure I understand how this is better than a define - if you know enough to know whether you're using the correct value, it can just as easily be a define? I think I can see the idea, but I'm probably not paranoid enough to understand how it's helpful XD
- tcoff91 2y agoJust in case anyone still doesn't understand what that means to be statically allocated. It means that they are allocated in the Data Segment, which is a separate area of virtual memory from the stack and the heap.
- levodelellis 2y agoOnly if it's const. Otherwise the data is copied into memory you can write to
- tcoff91 2y agoIsn’t there a part of the data segment that is writable? Initialized vs uninitialized data segment right?
- levodelellis 2y agoYou're right, I know mutable and read-only are separates, I know I seen gcc and clang put static int and global int in the same page, and I know I heard people say global variables are on the heap thousands of times. I guess I forgot global writable data doesn't necessarily mean heap nor the read-only section of memory
- Someone 2y ago> These variables are not on the heap. They are statically allocated. Their visibility is the only thing that differentiates them from global variables and static variables defined outside of functions. In C++, there is a difference: function-static variables get initialized when control first passes into the function. That difference matters in cases where the initial value (partly) comes from a function call or from another global. For example, in C++ one can do: int bar = -1; void foo() { static char * baz = malloc(bar); … } void quux() { bar = 13; } That’s fine as long as the code calls quux before ever calling foo. They added that feature not to support such monstrosities, but because they wanted to support having static variables of class type. If they were to design that today, I think they would require the initialization expression to be constexpr.
- j16sdiz 2y ago> If they were to design that today, I think they would require the initialization expression to be constexpr. Why would they? This (non-constexpr) semantic would be useful as lazy initialization. .. and C++ love tiny little features like these.
- billforsternz 2y agoSorry, I owe the author an apology for saying that they "cast shade" on the idea of returning the value of the static variable. Actually they, quite correctly, cast shade on the idea of returning the address of the static variable. I'd edit the original message, but I am (far) too late. I just noticed my mistake. All I can do is add this apology, for the record.
- levodelellis 2y agoI didn't mind. I knew I'd hear a lot of disagreements and incorrect thoughts. My next article has a lot of examples which should make things easier to digest. One thing I dislike about compsci is everyone has different definitions for everything. If people say static variables are not on the heap fine, but you can easily see the address of a static variable and global variable being in the same 4K page