Can the C variable be deleted or deleted at any time while the program is running?

I am wondering if it is possible to delete, that is, remove from use, the C variable, say, for example, as soon as the variable is used once?

I thought about this question some time ago, and one question arose in my mind.

C has many data types, and we can create a data type variable, such as integer, using simple code

int i;

Once we have created such a variable, how do we remove it if we do not use it in the future?

I searched on the net but did not find any C command that does this. By "k is deleted," I mean that "k has ceased to exist." I mean, when we do not need a variable, it is waste and should be removed.

C provides a free() function, but only works with memory allocated using calloc(), malloc() or realloc() .

So how to remove, say, an int variable, after using it?

+6
source share
2 answers

Not. The lifetime of an object ends when it goes out of scope (it is called an automatic object because it is automatically cleared).

eg.

 void foo() { int j = 3; while (blah) { int i = 5; } // i no longer exists } // j no longer exists 
+13
source

In C, there is the concept of the duration of storage of an object, which determines its lifetime:

  • static shelf life for the entire program execution
  • thread storage is maintained until the stream is completed
  • automatic storage is maintained until the surrounding block is closed.
  • dedicated storage needs explicit de-distribution via free()

The pending storage aside, the language runtime will take care of fixing the memory (for example, it reduces the stack pointer to drop call frames, which frees up the automatic storage).

+5
source

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


All Articles