-1

In the below link, answer given by Sdaz MacSkibbons gives a brief overview of process address space on virtual address systems.

What happens when a computer program runs?

Now suppose every process gets 4GB virtual address space. Now does that mean that the top addresses of this virtual address space will get stack part (Suppose stack address starts from 0) and address space from bottom will be allocated to text, data, globals and heap. Since size of heap changes, do new malloc allocations will map virtual address space pages to real memory directly or do they check whether previously allocated virtual address pages to heap are free/available. And How about if we need large amount of heap memory, like greater than 4GB, than how do process supports that ?

Thanks in advance.

Community
  • 1
  • 1
Shubham Jain
  • 1,864
  • 2
  • 14
  • 24
  • Depends on the OS. Create a C program using printf with %p to print out the addresses of local variables (stacK), versus allocated (malloc()) variables, versus addresses of static variables (program data), versus addresses of functions (pointer to function), ... . Part of the virtual address space is used for the OS. There's also some data related to each running thread (like the seed value for rand()) (FS or GS segment registers may be used to access this, or it could be mapped into virtual address space). – rcgldr Aug 03 '16 at 17:21

1 Answers1

1

The answer you link to simply shows a way things could be done.

Assuming you have a logical 4GB address space, not all of those might be valid or even potentially valid virtual addresses.

Some part of that logical address space will be devoted to the system. It is unlikely the system will use or come close to using the entire logical address range devoted to it.

There will be other areas in the logical address space that will not have valid logical addresses.

Next, describing memory as heap and stack is misleading. Heap and stack are simply read/write memory. There is nothing special about them and the operating system does not care what the memory is being used for.

Think of program sections as being: - Executable, readonly - No execute, read/write - No execute, readonly

Your second question is about malloc. Malloc implementations manage pools of read/write memory. Malloc tries to process a memory request by returning memory from the pool. If there is not sufficient memory available, malloc will increase the size of the pool by mapping more virtual memory to the logical address space.

If applications need to allocate large amounts of memory, they usually do not use malloc. They operating system services instead. If you have a 4GB address space, you cannot allocate blocks larger then 4GB.

user3344003
  • 20,574
  • 3
  • 26
  • 62