Is there a difference between the size of memory allocated for the following types of declarations:

i) static int a, b, c;

ii) int a; int b; int c; int a; int b; int c;

I'm not sure how the memory will be allocated for these types of declarations. And if these announcements differ, how much memory is allocated for each declaration?

+4
source share
4 answers
 static int a,b,c; 

allocates three ints (possibly 32 bits each or 4 bytes) in the DATA section of your program. They will always be there while your program runs.

 int a; int b; int c; 

will highlight three ints on STACK. They will disappear when they go out of scope.

+6
source

There is no difference between memory size for

 static int a,b,c; int a;int b;int c; 

Differences occur in life, location, region and initialization.

Lifetime: if they were announced globally, they would be a, b, c throughout the life of the program. If they were both in function, then static would exist for the lifetime of the program, while the others would exist only for the duration of the function. In addition, if the function is called recursively or repeatedly, there are many sets of non-static a, b, c.

Location A common, not required C, is the presence of a DATA section and a STACK section. Global variables, as a rule, relate to DATA, as well as to functional static. The non-static version of the function a, b, c in a function is usually included in STACK.

Scope: simple view: functionally declared variables (static or not) are limited inside the function. Static global variables are file sizes. Global variables that are not declared static are program-wide.

Initialization: follows the same path as the time of life. Declared globally declared a, b, c, static or not, both are initialized when the program starts. If a, b, c are in a function, only static ones are initialized (when the program starts). Functional non-static values ​​a, b, c are not initialized.

Optimization can affect location, especially for functional non-static a, b, c, which can be easily stored in registers. Optimization can also determine that a variable is not used and does not optimize it, taking 0 bytes.

+4
source

Variables that are defined as static will be highlighted in the data segment at compile time. The same is true for global variables, even if they are not static. Non-static variables defined in a block are allocated on the stack when the block is entered at runtime and deactivated when exiting this block.

The amount of allocated memory depends on the implementation. The standard requires that int be large enough to hold a 16-bit (2 byte) value, but may be larger. Most compilers that you most likely use now use 32-bit ints.

0
source

If we assume that the 2nd declaration is inside the function, then, as Bard and Nashant said, they will be allocated in different memory sections (OS and compiler dependent).

But although the variable size will be the same size, they MAY consume different amounts of memory. If a function (from the 2nd declaration) is called recursive, for example, there will be several instances of variables from the 2nd declaration.

0
source

Source: https://habr.com/ru/post/1492088/


All Articles