When is space allocated for local variables?

Example

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

When a program containing such code is executed, are they allocated a, band ceach time it is called Test, or are they distributed only once somewhere at the initialization stage? I ask about this because such information is not available in the debugger.

+5
source share
2 answers

Here is a more accurate version.

Local variables are allocated:

  • Usually on the stack
  • In the register, if the optimizer can use it: for example, a simple method with only a loop and var i: integerdeclared as a local variable will most likely allocate iCPUs as registers for better speed.

How is the stack distributed?

x86 x64 :

  • ;
  • (, a MOV EBP,ESP; SUB ESP,16);
  • , (, string); (, integer) , ;
  • try..finally, ;
  • /method;
  • finally /method: , ;
  • (, MOV ESP,EBP);
  • .

" " ( EBP): , .

result : CPU/FPU, , , .

x64 , , , , .

Mac OS .

/ , / inline : , .

. Delphi.

+3

. , .

+7

All Articles