From 5a03ea6fdeec6b0b01ede01764f1b189ce67199d Mon Sep 17 00:00:00 2001 From: Kunal Dangi <61555672+galaxone@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:29:24 +0530 Subject: [PATCH] Stack and Stack Overflow (#750) * 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 --- .../language/reference/04-Functions.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/scripting/language/reference/04-Functions.md b/docs/scripting/language/reference/04-Functions.md index 19b364ac4..ba3c84b88 100644 --- a/docs/scripting/language/reference/04-Functions.md +++ b/docs/scripting/language/reference/04-Functions.md @@ -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)