When is memory freed in c?

I am trying to figure out when the memory used for variables in c is freed. As an example, when would the integer i in the following code fragment be freed?

 int function() { int i = 1; // do some things return 0; } 
+4
source share
5 answers

In C, as in all other languages, lexically copied variables, such as i here, are valid only within their scope - area i is its declaration through the closing shape of the function. When exactly they are freed, it is often not specified, but in practical C implementations, local variables are allocated in the call stack and their memory is reused after the function returns.

Consider something like

 int function() { int i; // beginning of i scope { int j; // beginning of j scope ... } // end of j scope { int k; // beginning of k scope ... } // end of k scope return 0; // all locals of the function are deallocated by the time it is exited } // end of i scope 

The scope determines which variables can be accessed by name and for local ( auto ) variables when their content can be actually accessed (for example, if you pointed to the address of a local variable by dereferencing the pointer outside the scope of undefined). Releasing is a slightly different matter ... most implementations will not do anything at the end of j or k scope to โ€œfreeโ€ them, although they are likely to reuse the same memory for both variables. When function returns, most implementations will โ€œsqueezeโ€ all locales from the stack along with the return address with one decrement of the stack pointer, effectively โ€œfreeingโ€ their memory ... although the memory still remains there on the stack, ready to be "allocated" for the next function to be called.

Note that the terminology of your question is somewhat confusing ... variables have scope, but this is memory, not variables that are allocated and freed. Some variables may not even have memory allocated for them if, for example, they are constants or are never used in a program. And only the memory for local variables is allocated or freed up, as described above ... the memory for variables of the static and file areas is never freed and is allocated only when the program is loaded. And there is another memory - a bunch of memory that is explicitly allocated and freed by your program (via calls to malloc / realloc / calloc / strdup / free, etc.). But although heap memory can refer to pointer variables, the memory for pointer variables themselves consists only of a reference (memory address), and the variables have either a local or a static / file area.

+9
source

He will be released when he goes beyond. Since it has a function area, this will happen when the function returns.

+7
source

i is allocated on the stack. It is exempted upon return.

+1
source

Automatic variables are local to a region in C:

The scope is basically labeled '{' '}', so when you are inside the pair {}, you are inside the scope. Of course, areas can be nested.

This is why in the earlier C standard, local variables had to be defined at the top of the scope, because it made it easier for the C compiler to write (the compiler would see "{" and then all the variables and then the instructions), so he knew the variables with which he had to deal. This has changed in C99 (I think?), And later, where you can define variables anywhere between statements.

Of course, this can be useful:

 int foo() { int k = 0; /* inside the scope of foo() function */ for(; k < 10; k++) { /* don't need k = 0; here we set it to zero earlier */ int i = 1; /* initialize inside the scope of the for loop */ i = i * 2; /* do something with it */ printf ("k = %d, i = %d\n", k, i); } #if 0 printf ("i = %d\n", i); /* would cause an unknown identifier error * because i would be out of scope, if you changed * #if 0 to #if 1 */ #endif return 0; } int main() { foo(); foo(); return 0; } 

Note that I = 2 all the time for loop iterations in foo ()

It's even more interesting to see how the static changes the persistence of a variable.

 int bar() { int k = 0; /* inside the scope of bar() function */ for(; k < 10; k++) { /* don't need k = 0; here we set it to zero earlier */ static int i = 1; /* initialize inside the scope of the for loop */ i = i * 2; /* do something with it */ printf ("k = %d, i = %d\n", k, i); } #if 0 printf ("i = %d\n", i); /* would cause an unknown identifier error * because i would be out of scope, if you changed * #if 0 to #if 1 */ #endif return 0; } int main() { foo(); foo(); return 0; } 

Notice the changes and notice how the static variable is handled.

What happens to the for loop if you put the static keyword before int k = 0 ;?

Interesting use

Macros

C is very useful. Sometimes you want to define a complex local macro rather than a function for speed and overall simplicity. In the project, I wanted to manipulate large bitmaps, but using functions to perform "and" or "xor" operations was a little painful. The size of the bitmaps was fixed, so I created several macros:

 #define BMPOR(m1, m2) do { \ int j; \ for (j = 0; j < sizeof(bitmap_t); j++ ) \ ((char *)(m1))[j] |= ((char *)(m2))[j]; \ } while (0) 

do {} while (0) - a funny trick to associate a block with binding to if / for / while, etc. without a problem, the block is executed exactly once (since while is checked in the END block), and when the compiler sees while (0), it simply removes the loop; and, of course, this allows you to put a half-year at the end so that the IDE and you (later) do not get confused about what it is.

The macro above was used as:

 int foo() { bitmap_t map_a = some_map(), map_b = some_other_map(); BITOR(map_a, map_b); /* or map_a and map_b and put the results in map_a */ } 

here do {} while (0) allows a local scope with local variable j, a for loop, and all that I might need.

+1
source

A variable becomes freed when the variable goes out of scope. In your code variable, i will be freed after executing return 0 . You can find more in the scope variable here

0
source

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


All Articles