Home > Software design >  Virtual memory and Heap
Virtual memory and Heap

Time:05-04

Good morning. I hope some can help me out understanding how one aspect of virtual memory works and how C behaves. From what I understand, whenever we call malloc, C will add it to the heap, with the pointers going upwards. If the stack and the heap bump on each other, malloc will return NULL, since there is no more memory to work with. Virtual memory map

What I do not understand is the fact that the Virtual memory of each program is seized when we call it, and the low and high adresses of the runing script itself are determined. This way, the program has a fixed amount of memory to use. Is the heap growing with the data on it, or the heap is actually just a set of pointers to the actuall data? If the program has a fixed memory at the begin (because it can´t have all the memory) for me it does not make sense to store the raw data in the heap, or else we easily would get out of available memory. What am I missing?

CodePudding user response:

You are making several incorrect assumptions. The most important one being that you program has one chunk of memory assigned to it that starts at address x and goes to address x program size. This is not so, your program is divided into chunks (different platforms give them different names). The stack will be one, the heap may be several, the code will be in several etc.

When the heap manager runs out of its current chunk it can simply get another one.

Also note that this has nothing to do with 'virtual memory'.

  • Related