Are variables declared within blocks or subprograms dynamically allocated?

I am reading the book "Algorithms and programming with examples in Pascal and C" and in it there are two paragraphs that left me in doubt, they are:

Two alternative ways are offered by some programming languages to manage dynamic allocation: (1) declaration of variables within blocks (Chapter 4) or in subprograms (Chapter 9); and (2) allocation of space by means of pointers.

In dynamic allocation involving variables declared within of blocks or subprograms, variables Store application values in the same way as static variables, but they are allocated by the system when starting the execution of the block or subprogram and only persist for the duration of the execution of the same. Memory areas associated with variables declared in this mode are automatically released by the system.

According to the author, the declaration of variables within blocks or in subprograms involves dynamic allocation. This correct this statement? In my understanding, only allocation via pointers is considered dynamic allocation.

Author: Comunidade, 2018-03-06

2 answers

The term dynamic allocation is not often used in this context. In a way it's even true. This type of allocation occurs in the stack, so it always occurs at runtime, in this aspect we can say that the allocation is dynamic, as opposed to being static when the data is already in binary code and the allocation was made at compile time. But if you think about this data, it is not dynamic because the effective allocation only occurs in the load of the executable.

The term it fell a little out of use, but the correct one for allocation in the stack is automatic allocation, thus differentiates from static. In a sense the memory there is static since the whole stack usually has fixed size and allocated in advance. Then there is a general prior allocation on the load of the executable and then an internal allocation during execution that every stack frame .

For all this it is considered that these allocations are always static or automatic, and the dynamic allocation is the one you ask to allocate in the heap and it is accessed by value or through a reference somewhere.

Dynamic allocation needs some management while static allocation is managed automatically. This management can be automatic through a garbage collector(garbage collector) or manual .

The term "static variables" is completely wrong. The allocation can have this characteristic, a variable no.

Dynamic allocation does not necessarily have to be through pointers, but it almost always ends up being accessed like this. But in a way All Access is done like this, is that in some cases the address of the Pointer is already in the code and probably the processor makes access without cost or even transparent. Everything is done with indirection .

In the automatic memory nothing is released, it just stops using that space.

Therefore within of what is used in our area the author is wrong, as well as the answers posted here (which even err on points that are not even the focus of the question). The author speaks of scope and lifespan .

 2
Author: Maniero, 2020-07-13 16:57:56

Is correct, Alocação Dinâmica in the context cited by the author refers to the fact that an existing variable in a block of code, be it a function, for, while, if, etc. It exists only within that code scope. That is, it is created when that scope is executed, and (possibly) released from memory when its execution ends.

Parsing a basic example:

function foo() {
   var a = 4; // existe no escopo de foo() e bar().

   function bar() {
      var b = 5; //existe no escopo de bar(), não existe no escopo de foo();
   }
 // var b não existe aqui..
}
// var a e b não existe aqui...
 0
Author: Israel Merljak, 2018-03-06 17:39:22