Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is space allocated for local variables?

Tags:

delphi

Example

function Test: Boolean;
var
  a, b, c: Integer;
begin
  ...
end;

When a program containing such code is executed, are a, b, and c allocated each time Test is called, or are they allocated only once somewhere in the initialization phase of the execution? I ask this because such an information is not available in the debugger.

like image 380
Astaroth Avatar asked Dec 21 '10 05:12

Astaroth


People also ask

How are local variables allocated?

2. Allocation is the generation of memory storage for the local variable. The computer allocates space during execution by decrementing the SP. In this first example, the software allocates the local variable by pushing a register on the stack.

When memory is allocated for local variables in Java?

Java Runtime creates Stack memory to be used by main() method thread when it is found at line 1. At line 2, a primitive local variable is created, which is stored in the Stack memory of main() method. Since an Object is created at line 3, it's created in Heap memory and the reference for it is stored in Stack memory.

When memory is allocated to a variable?

When a variable is declared compiler automatically allocates memory for it. This is known as compile time memory allocation or static memory allocation. Memory can be allocated for data variables after the program begins execution. This mechanism is known as runtime memory allocation or dynamic memory allocation.

Is memory allocated during declaration or initialization?

Memory is allocated when a variable is declared, not when it's initialized.


1 Answers

Local variables are created in the stack, after the call to the function. They are removed by the called function by default when the function returns.

like image 80
Ignacio Vazquez-Abrams Avatar answered Sep 29 '22 17:09

Ignacio Vazquez-Abrams