1

Process sections

When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─ stack, heap, text and data. The following image shows a simplified layout of a process inside main memory.

I was wondering what does the blank part means, and what does the arrow means. if anyone can help with this. thank you so much.

1 Answers1

1

That area is usually used by the stack section, because the stack can grow/shrink based on the depth of current call stack. as your link states that

The process Stack contains the temporary data such as method/function parameters, return address and local variables.

How the depth of call stack grows?

To really understand this you would have to know how assembler deals with function calls. but I will try to provide a simple explanation. So when your code calls a function, its associated data is pushed to stack and the stack grows downward and when the execution of function completes, its local data is no longer needed so the stack shrinks back.

But when your code contains nested function calls, that is function A calls function B and function B calls function C. in this case stack keeps growing.

And if your code contains too many nested function calls, stack of your process may expand such that there is no more empty area. when that happens (probably when you implement a recursive function without correct base case), we get our beloved stack overflow error.

Following section provides a visual explanation of this process

let's say you are running a program and in pseudo code it looks something as following

fun first(arguments){
   second();
}

fun second(arguments){
   third();
}

fun third(arguments){
   print("hello");
}

fun main(){
   first(arguments);  // line 1
   third(arguments);  // line 2
}

When your program starts, its memory block would look something as following.

enter image description here

Because as your program starts it calls the main method, the stack will hold all the local variable associated with the main function.

When main function calls the first function, stack will grow downward as local variable and other stuff related to first function would be pushed to stack. so it look as following.

enter image description here

because first function internally calls second and second function internally calls third the stack will expand to accommodate all the data associated with second and third functions.

Please note that stack would only grow for nested function calls. for example when first line in main function has executed and the execution moves to line 2, stack will decrease in size.

mightyWOZ
  • 7,946
  • 3
  • 29
  • 46