cvariablesmemory

location of non-local variables of a procedure or function in C


In C, can non-local function and procedure variables be found in the heap and static zone? in the heap those allocated dynamically, i.e. using malloc and calloc. In the static area, however, there are global variables accessible from all points of the program, right? in the stack instead there are the local variables of the functions or procedures right?


Solution

  • Where objects are stored by C implementations is complicated; it is not solely determined by whether a variable is “local” or not. Two variables defined inside a function, with the same scope for their names, may be stored in different places because one is declared with static and the other is not.

    The C standard does not define any variables as “local” or “global.” It does not even use these words to describe variables except one passing instance related to the names of the parameters of main. And the standard does not use the word “procedure” at all.

    In C, a variable is composed of two parts: An identifier (name) and memory that is used for the variable. The memory is called an object.

    Regardless of where a variable is declared or how an object is created, an object is accessible throughout a program, for the duration of the object’s lifetime. In the C standard, “access” means to read or write an object, and any part of a program can read or write an object if it has the object’s address (except that writing to an object defined with const is not defined).

    If you are concerned about “local” and “global” variables, you are likely actually concerned with the scope of the identifier. The scope is the region of program source code where the identifier can be used to refer to its associated object (except when it is hidden by another declaration of the same identifier in an inner scope).

    In ordinary general-purpose C implementations, where an object is stored is determined by its storage duration. C has four storage durations:

    The above is a common scheme used for C implementations, but the C standard permits other implementations.

    Note that “local” may be an apt description of identifiers with block scope, but “global” is not entirely correct for any identifiers in C other than keywords. C has identifiers with file scope (the name is known throughout its translation unit from the point of declaration on) and external linkage (different declarations of the name in different translation units can be linked to the same entity) but does not have any global identifiers. For an identifier to be truly global, declaring it once in the program would have to make it known throughout the program. C does not have that; to use an identifier in multiple translation units, it has to be declared separately in each unit.