Was this helpful?
Stack Overflow
A working knowledge of how stacks interact with software and the operating system is beneficial. Here is an overview in relation to data transformations. Pay special attention to the section regarding how to handle the returned value from functions.
Stack is a data structure, a temporary space in memory that is used by the program to hold data to be processed. Memory is allocated as needed to accommodate items placed in stack, and stack must be emptied of its contents during the course of its current use. Stack operates on a LIFO principle, "last in, first out".
As an example, think of stack as a filing tray. You place documents in the tray because you know in advance that you will need all of these documents to complete your current task and you want to be able to retrieve them quickly when they are needed.
If you attempt to place another document in an already full tray, you encounter "stack overflow". Stack overflow can also occur if you repeatedly forget to handle something that is causing the stack to get larger and larger, causing it to eventually overflow.
One common cause of stack overflow is when a function is treated as a subroutine. The best workaround is to declare a temporary variable to hold the return of this function even if that return is not used in a subsequent expression.
If you reach for the filing tray to remove a document and you have already used all of them, there is a shortage of documents to complete your process. If this happens in stack, an error is generated. This error is called "stack underflow".
Note:  If a function returns a value, the value must be used. If the returned value is not used in a subsequent expression or elsewhere in the transformation, then it must be stored. Storing the value satisfies the use requirement.
So, you may ask, "How do I prevent my expressions from generating stack overflow errors?" Two examples follow.
Example 1 (Incorrect)
This example causes a stack overflow:
For x = 1 to 1000
  Str(x)
Next x
The Str function is called, but its value is not used.
Example 2 (Correct)
This is the proper way to write this expression:
For x = 1 to 1000
  y = Str(x)
Next x
Although the variable y is not used elsewhere in the transformation, it serves as a place to hold the return of the Str function. It is not placed in the stack, which prevents this expression from causing stack overflow.
64-Parameter Limitation in Function Arguments
Function parameters are passed on the stack. If the stack is empty, the number of parameters that can be passed as arguments has a theoretical maximum of 64. However, if other processes, especially iterative structures, such as For loops, are also using memory stack resources, then this maximum is reduced. If stack resources are insufficient, you may receive a stack overflow error message.
For more information on the use of stacks as data structures, see http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?stack.
Last modified date: 12/03/2024