Are variables declared within blocks or subprograms dynamically allocated?

Asked

Viewed 138 times

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, are they:

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 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 the execution of the block or subprogram and only persist as long as the execution of it lasts. 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 statement correct? In my understanding, only pointer allocation is considered dynamic allocation.

  • 2

    This question @Maneiro answers all your questions. I don’t know to what extent it won’t be duplicated

  • @Isac, the post you refer to doesn’t answer "all" questions I’ve asked here. My questioning is more related to the terms "dynamic allocation" or "automatic allocation" than the concept of.

  • If that’s the case, I don’t think you made that very clear in the question.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

3

The term dynamic allocation is not usually used in this context. In a way it is true. This type of allocation occurs in the pile, then 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 it until this data is dynamic because the effective allocation only occurs in the executable load.

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

All this is why these allocations are always static or automatic, and dynamic allocation is the one you ask for allocate into heap and it is accessed by value or through a reference somewhere.

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

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

Dynamic allocation does not necessarily have to be through pointers, but almost always ends up being accessed like this. But in a certain 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 without cost or even transparent. Everything is done with indirect.

In automatic memory is never released anything, just stops using that space.

So within what is used in our area the author is wrong, as well as the answers posted here (which even make mistakes in points that are not the focus of the question). The author speaks of life span and time.

  • thanks for the clarifications. Briefly, I believe that the correct term, in this case, would be "automatic allocation" since allocation occurs in the stack and resources are automatically released at the end of the function or block.

  • @Rodrigocosta Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

0

That is correct, Alocação Dinâmica in the context cited by the author refers to the fact that a variable existing in a block of code, is a function, for, while, if, etc. It exists only within that scope of code. That is, it is created when that scope is executed, and (possibly) released 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...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.