Featured post
delphi - When is space allocated for local variables? -
example
function test: boolean; var a, b, c: integer; begin ... end; when program containing such code executed, a, b, , c allocated each time test called, or allocated once somewhere in initialization phase of execution? ask because such information not available in debugger.
here more exact version.
local variables allocated:
- usually on stack;
- in registers if optimizer can use it: instance, simple method loop ,
var i: integerdeclared local variable allocateicpu register, better speed.
how stack allocated?
on both x86 , x64 scheme, compiler has same process:
- it first computes space needed, @ compile time;
- it generates code reserve space on stack (e.g.
mov ebp,esp; sub esp,16); - it generates code initialize reference-counted variables allocated on stack (e.g.
string) - other kind of variables (likeinteger) have no default value, , can random content on stack; - it generates hidden
try..finallyblock if there reference-counted variables; - it generates code internal of function/method;
- now here
finallypart of function/method: generates code free reference-counted variables; - it generates code release space on stack (e.g.
mov esp,ebp); - it generates code return caller function.
most of time, "stack frame" (pointed register ebp) created: used access directly variables allocated on stack.
there specific handling of result variable of function: sometimes, cpu/fpu register, sometimes, variable initialized caller, , passed additional parameter.
on x64, bit more complicated, since exceptions not handled same, , registers need have space allocated on stack, if there inner call.
on mac os, there alignment issues.
all stack allocation / initialization process reason why small functions/methods, declaring them inline make code faster execute: stack handling slow, if process within function simple.
- Get link
- X
- Other Apps
Comments
Post a Comment