Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local variables: are they always on the stack?

Tags:

delphi

In the following procedure, will the array be allocated on the stack?

procedure One:
var
  arr: array[0..1023] of byte;
begin
end;

What is the largest item that can go on the stack?

Is there a speed difference between accessing variable on the stack and on the heap?

like image 744
Michael Vincent Avatar asked Jan 25 '16 12:01

Michael Vincent


People also ask

Are local variables stored on the stack?

The stack is used for dynamic memory allocation, and local variables are stored at the top of the stack in a stack frame. A frame pointer is used to refer to local variables in the stack frame.

Why are local variables on the stack?

If local variables are required, they are allocated on the stack. By adjusting the rsp register, additional memory is allocated on the stack for locals. As such, when the function is completed, the memory used for the stack-based local variables is released (and no longer uses memory).

Where are local variables always allocated?

Advantages of using Stack When a function is called the local variables are stored in a stack, and it is automatically destroyed once returned. A stack is used when a variable is not used outside that function. It allows you to control how memory is allocated and deallocated.

Are local variables stored on the stack Java?

Stack in java is a section of memory which contains methods, local variables, and reference variables. Stack memory is always referenced in Last-In-First-Out order. Local variables are created in the stack.


1 Answers

In the following procedure, will the array be allocated on the stack?

Yes, provided that the local variable is not captured by an anonymous method. Such local variables reside on the heap.

What is the largest item that can go on the stack?

It depends on how large the stack is, and how much of the stack has already been used, and how much of the stack is used by calls made by the function itself. The stack is a fixed size, determined when the thread is created. The stack overflows if it grows beyond that size. On Windows at least, the default stack size is 1MB, so I would not expect you to encounter problems with a 1KB array as can be seen here.

Is there a speed difference between accessing variable on the stack and on the heap?

By and large no, but again this depends. Variables on the stack are probably more likely to be accessed frequently, and so probably easier to be cached. But for a decently sized object, like the 1KB array we can see here, I would not expect there to be any difference in access time. In terms of the underlying memory architecture, there's no difference between stack and heap, it's all just memory.

Now, where there is a difference in performance is in allocation. Heap allocation is more expensive than stack allocation. And especially if you have a multi-threaded application, heap allocation can be a bottleneck. In particular, the default Delphi memory manager does not scale well in multi-threaded use.

like image 92
David Heffernan Avatar answered Sep 26 '22 00:09

David Heffernan