Stack and Heap : Is there a single stack and a single heap area in memory and grows to each other ? (x86 linux)

-3

I try to figure things out about stack (stack pointer ESP , EBP ... etc) and heap.

As i know the kernel memory space is 1GB at the end of the memory (0xC0000000 to 0xFFFFFFFF) and right above it is the stack memory , so my question is :

Is there one "big stack" and every userspace program use frames and the stack pointer is initialized by OS , i mean the OS sets the stack pointer for a particular program and for every other programs in the switching context just saved the stack pointer and heap pointers , all registers right ?)

I know these addresses are virtual and i guess every program have (almost) same addresses that are mapped to different pages in the real memory , but this is the big picture right ?

I mean it's impossible for a program to exit it's allowed memory for the stack so he can overlap stack pointer to portions of memory and stuff .

I want to be able to get the whole picture and if anyone knows some material, videos, courses, books that can en-light the whole picture of memory architecture with the OS + running apps would be amazing .

Thanks

memory
memory-management
linux-kernel
stack
heap
asked on Stack Overflow Feb 24, 2019 by 123onetwothree • edited Feb 24, 2019 by 123onetwothree

1 Answer

0

There is not one big stack. Each thread in a program has it's own stack. Meaning a single program in an OS can have multiple stacks. Meaning a running OS can have many, many stacks.

The stack and heap memory are just abstractions; they both exist in a single entity called main memory. A program allocates a certain amount of main memory to a stack, and if that stack is full what happens is called a stack overflow. So it can't grow towards the heap, because if we fill up the stack the program will crash.

As for Kernel Memory Space, I have know idea where you got that 1 GB figure as it can depend on several factors.

For a very helpful video that might help clear up some of your misconceptions, this very well presented video by an EA game dev would probably help clear up some of your misconceptions: https://www.youtube.com/watch?v=wJ1L2nSIV1s&t=121s If you wanted to go more in depth, there are several operating systems courses on Coursera.

answered on Stack Overflow Feb 24, 2019 by user3586940

User contributions licensed under CC BY-SA 3.0