Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to call a static variable local?

It is known that the C language supports two kinds of memory allocation through the variables in C programs:

1) Static allocation is what happens when you declare a static variable. Each static variable defines one block of space, of a fixed size. The space is allocated once, when your program is started, and is never freed.

2) Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable. The space for an automatic variable is allocated when the compound statement containing the declaration is entered, and is freed when that compound statement is exited.

(this is a full quote from http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_3.html)

The question is: is it correct to call a static variable in a function "local" in terms of memory allocation and why? Thanks to everyone in advance.

P.S. any quotes from the C standard are welcome.

like image 662
VoidWalker Avatar asked Nov 24 '25 14:11

VoidWalker


2 Answers

C standard doesn't define the term of local variable. Automatic and static refer to storage duration.

C11 (n1570), § 6.2.4 Storage durations of objects

An object has a storage duration that determines its lifetime.

like image 77
md5 Avatar answered Nov 27 '25 04:11

md5


You could call it a "function-local static variable" or something like that, but if you simply call it a "local variable" you may find that people are surprised when they find out it's actually static, and therefore has some of the properties of a global variable.

like image 39
John Zwinck Avatar answered Nov 27 '25 05:11

John Zwinck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!