Are variables declared within blocks or subprograms allocated dynamically?

1

I'm 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 forms 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 blocks or subprograms, variables store application values in the same way as static variables, but they are allocated by the system when starting execution of the block or subprogram and only persist for the duration of execution the same. The 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. Is this correct? In my understanding, only pointer allocation is considered dynamic allocation.

    
asked by anonymous 06.03.2018 / 17:23

2 answers

2

The term dynamic allocation is often not used in this context. In some ways it's true. This type of allocation takes place in the stack , so it always occurs at run time, in this respect we can say that the allocation is dynamic, as opposed to being static when the data is already in the binary code and the allocation was done at compile time. However, if you think well, this data is still dynamic because the effective allocation only occurs in the executable load.

The term has dropped a bit in disuse, but the correct one for the allocation on the stack is automatic allocation , so it differs from static. In a sense the memory there is static since the whole stack is usually fixed and allocated in advance. Then there is a general prior allocation in the executable load and then an internal allocation during execution that each stack frame .

By all this we consider that these allocations are always static or automatic, and the dynamic allocation is the one you ask for allocate in 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 automated through a garbage collector ( garbage collector ) or < a href="https://en.stackoverflow.com/q/43766/101"> manual .

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

Dynamic allocation does not necessarily have to be through pointers, but almost always ends up being accessed as well. 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 the access free or even transparent. Everything is done with indirection .

In automatic memory, nothing is ever released, just stop using that space.

Therefore, within the area of 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 talks about scope and lifetime .

    
07.03.2018 / 01:43
0

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

Analyzing 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...
    
06.03.2018 / 18:32