User Tools

Site Tools


k7:k7.3:k7.3.2:start

7.3.2 Class Stack (gb. data)

The Stack class implements a stack of elements. This class uses the data type Variant for its elements.

A stack is a data structure that functions according to the LIFO principle. The principle “Last in, first out” describes abstractly what can be imagined, for example, as a stack of paper in a box. If you want to add a sheet of paper to the stack, you have to place it on top of the stack. The effort to lift parts of the stack to place the paper anywhere is simply too great. For this reason, it is also possible to remove only the topmost sheet of paper from the pile. The last added element is the first removed element (LIFO). In this sense, the stack is a data structure opposite the queue. The last element of a stack is also referred to as its topmost element. It is characteristic for the stack that it is only possible to operate at the top of the data structure - add and remove elements.

7.3.2.1 Properties

The properties of a stack are shown in the following table:

Property Data typeDescription
.SizeInteger (ReadOnly)Number of elements of the Stack
.IsEmptyBoolean (ReadOnly)True if there are no elements in the stack, otherwise false

Table 7.3.2.1.1: Properties of the class stack

7.3.2.2.2 Methods of a stack object

With these 4 methods, a stack can be used as a data structure according to the LIFO principle:

MethodDescription
Clear ()Remove all elements from the stack (Stack)
Push (vElement)Place an element (top) on the stack
Pop ()Remove and return the topmost element from the stack
Peek () Return of the topmost element from the stack - without removing it

Table 7.3.2.2.2.1: Methods of the class stack

7.3.2.3 Project for using the class stack

The Gambas interpreter also manages a stack on which all function calls are logged. This information can prove invaluable in the event of a bug in the IDE, for example in case of a program crash, to identify and fix the bug. You can better understand how the error occurred.

The stack of the Stack class in the project stores all functions that were called in the current code path. A code path is a chain of function calls. The associated stack is also called a “Call trace” or “Backtrace”. A backtrace is created using the following simple algorithm:

  • First, when entering a function, an entry for this function is pushed onto the backtrace stack? push (vElement),
  • then the function is executed and
  • finally the information has to be removed from the stack when leaving the function? pop ().

A function that calls another function gets the stack back in the state it was before the function call. However, if a program error occurs in a nested function, the backtrace stack can be examined. Then all functions can be identified that were called in the error-creating code path because their stack elements could not yet be removed. The most recently called function, which causes the error directly, is therefore always the topmost element of the backtrace stack.

A backtrace is a classic example of a stack. In the project for the stack class, such a backtrace is created:

Anwendung
Figure 7.3.2.3.1: Backtrace of the application program (blue) and the interpreter (red) in case of a runtime error

In addition to backtraces, stacks are also used for the implementation of parsers:? http://de.wikipedia.org/wiki/Umgekehrte_Polish_notation

Download

Project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k7/k7.3/k7.3.2/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools