r/C_Programming • • Jul 24 '26

Where do stack and heap start

I apologise in advance for a dumb question and I will try to put it the way i could and I am sorry for that.

I know i chould chatgpt it, but learning from people helps me the best.

want to know how the memory is actually managed. I know it is managed by the kernel using different abstractions like virtual memory and paging.

My question arises when i think that each process that its own address space consisting stack frame, heap, globals vars etc.

But where do they even start? Kernel itself is a process so it must be having its own stack frame. Considering other programs need address space which is managed by kernel, like how does that work? Until Os is loaded, does stack and heaps exist even before that? Are they written in assembly?

I am sorry. I hope someone will be able to answer and i will be really glad if you could figure out what i am thinking wrong and missing.

40 Upvotes

24 comments sorted by

View all comments

Show parent comment

1

u/Sufficient-Bee5923 Jul 29 '26 edited Jul 29 '26

At compile time the amount of memory needed for the specific scenario wasn't always known.

For example: depending on the system configuration, the amount of memory required would vary depending on the subscriber database size or other factors. This was common when we were creating memory pools blocks (more pool blocks needed in larger systems that also had more memory cards installed). By doing this, we had one binary image for all system configurations.

Static memory was also used in smaller fixed known situations and smaller embedded systems.

1

u/HoiTemmieColeg Jul 29 '26

Ah gotcha. So you essentially used it like a variable amount of static memory determined at runtime. Very neat!

2

u/Sufficient-Bee5923 Jul 30 '26

Exactly. Another golden rule, you can only allocate at system startup. You want don't want to find out later that there's not enough memory for this configuration. That makes software validation difficult.

Allocate or define everything at the start, if you run out, abort with good error handling.

1

u/clickyclicky456 Jul 29 '26

Yes, I've worked in very similar scenarios where that is the only way to manage.