Skip to content

Commit

Permalink
Stack and Stack Overflow (#750)
Browse files Browse the repository at this point in the history
* Stack and Stack Overflow

Added a topic stack memory and stack overflow under docs/scripting/language/reference/04-Functions

* change the image url to imgur

* fixed image not found
  • Loading branch information
kunaldangi committed Aug 8, 2023
1 parent c535968 commit 5a03ea6
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/scripting/language/reference/04-Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1348,4 +1348,27 @@ factorial(3) \\ factorial initiate
if(0 == 0) return 1 \\ checks the conition which is true and return 1
\\ at the final call 3 * 2 * 1 * 1
```
### Stack Memory
The stack is a region of memory used for storing local variables, function call information, and control flow data. It operates in a Last-In-First-Out (LIFO) manner, which means that the last item pushed onto the stack is the first one to be popped off.
#### Example (Stack Overflow)
```c
#pragma dynamic 35 // (35 * 4 bytes, a cell size) #pragma dynamic [cells] helps to modify the size of stack, read docs/scripting/language/Directives to know more about #pragma
main(){
grow_stack(1);
}
grow_stacK(n){ // recursive function
printf("N: %d", n);
grow_stacK(n+1);
}
```
#### Output
```
N: 1
N: 2
N: 3
.. .
Stack/heap collision (insufficient stack size)
```
![Stack](https://i.imgur.com/ZaIVUkJ.png)

[Go Back to Contents](00-Contents.md)

0 comments on commit 5a03ea6

Please sign in to comment.