What is this "stack" and why there is an overflow there using the code above?function DoStackOverflow : integer; begin result := 1 + DoStackOverflow; end;
So, the DoStackOverflow function is recursively calling itself - without an "exit strategy" - it just keeps on spinning and never exits.
A quick fix, you would do, is to clear the obvious bug you have, and ensure the function exists at some point (so your code can continue executing from where you have called the function).
You move on, and you never look back, not caring about the bug/exception as it is now solved.
Yet, the question remains: what is this stack and why is there an overflow?
Memory In Your Delphi ApplicationsWhen you start programming in Delphi, you might experience bug like the one above, you would solve it and move on. This one is related to memory allocation. Most of the time you would not care about memory allocation as long as you free what you create.
As you gain more experience in Delphi, you start creating your own classes, instantiate them, care about memory management and alike.
You will get to the point where you will read, in the Help, something like "Local variables (declared within procedures and functions) reside in an application's stack." and also Classes are reference types, so they are not copied on assignment, they are passed by reference, and they are allocated on the heap.
So, what is "stack" and what is "heap"?
Stack vs. HeapRunning your application on Windows, there are three areas in the memory where your application stores data: global memory, heap and stack.
Global variables (their values / data) are stored in the global memory. The memory for global variables is reserved by your application when the program starts and remains allocated until your program terminates. The memory for global variables is called "data segment".
Since global memory is only once allocated and freed at program termination, we do not care about it in this article.
Stack and heap is where dynamic memory allocation takes place: when you create a variable for a function, when you create an instance of a class, when you send parameters to a function and use/pass its result value, ...
StackWhen you declare a variable inside a function, the memory required to hold the variable is allocated from the stack. You simply write "var x : integer", use "x" in your function, and when the function exits, you do not care about memory allocation nor freeing. When the variable goes out of scope (code exits the function), the memory which was taken on the stack is freed.
The stack memory is allocated dynamically using the LIFO ("last in first out") approach.
In Delphi programs, stack memory is used by
- Local routine (method, procedure, function) variables.
- Routine parameters and return types.
- Windows API function calls.
- Records (this is why you do not have to explicitly create an instance of a record type).
Stack memory size is, by default, large enough for your (as complex as they are) Delphi programs. The "Maximum Stack Size" and "Minimum Stack Size" values on the Linker options for your project specify default values - in 99.99% you would not need to alter this.
Think of a stack as a pile of memory blocks. When you declare / use a local variable, Delphi memory manager will pick the block from top, use it, and when no longer needed it will be returned back to the stack.
Having local variable memory used from the stack, local variables are not initialized when declared. Declare a variable "var x : integer" in some function and just try reading the value when you enter the function - x will have some "weird" non-zero value. So, always initialize (or set value) to your local variables before you read their value.
Due to LIFO, stack (memory allocation) operations are fast as only a few operations (push, pop) are required to manage a stack.
HeapA heap is a region of memory in which dynamically allocated memory is stored. When you create an instance of a class, the memory is allocated from the heap.
In Delphi programs, heap memory is used by / when
- Creating an instance of a class.
- Creating and resizing dynamic arrays.
- Explicitly allocating memory using GetMem, FreeMem, New and Dispose()
- Using ansi/wide/unicode strings, variants, interfaces (managed automatically by Delphi).
When you ask for a new memory block (i.e. create an instance of a class), Delphi memory manager will handle this for you: you'll get a new memory block or a used+discarded one.
The heap consists of all virtual memory (RAM and disk space).
All clear?Now that all about memory is clear, you can safely (in most cases) ignore the above and simply continue writing Delphi programs as you did yesterday :)
Of course, you should be aware of when and how to manually allocate / free memory.
The "EStackOverflow" (from the beginning of the article) was raised because with each call to DoStackOverflow a new segment of memory has been used from the stack and stack has limitations. As simple as that.