How does the compiler enforce the stack memory to be contiguous, does it cause the memory to be moved everytime while the program is running or does it reserve the memory on stack needed by program before running it?
The stack for a given thread is often contiguous in virtual memory (on Linux and similar systems, and in user mode in Windows). The Windows kernel (in Windows Vista and above) and z/OS allow discontiguous stacks in virtual memory, and GCC 4.6 will also allow that.
When we talk about memory or disc allocation, the word "contiguous" simply means "without any gaps". A single stack or heap memory allocation is always contiguous ... in every programming language runtime I've ever encountered where it makes sense to talk about allocations at all.
4.4 Stack Memory Operations. Stack memory is a memory usage mechanism that allows the system memory to be used as temporary data storage that behaves as a first-in-last-out buffer. One of the essential elements of stack memory operation is a register called the Stack Pointer.
What is contiguous memory? Consecutive blocks of memory allocated to user processes are called contiguous memory. For example, if a user process needs some x bytes of contiguous memory, then all the x bytes will reside in one place in the memory that is defined by a range of memory addresses: 0x0000 to 0x00FF.
What is Stack Memory: The allocation that takes place on contiguous memory blocks. We call it Stack memory assignment since the assignment takes place in the function call stack. The memory to be assigned is known for compiling, and when a feature is called, their variables will be assigned memory to the stack.
We call it stack memory allocation because the allocation happens in function call stack. The size of memory to be allocated is known to compiler and whenever a function is called, its variables get memory allocated on the stack. And whenever the function call is over, the memory for the variables is deallocated.
For embedded applications with embedded OS, each task might have its own stack memory range (see Figure 4.15 from Chapter 4 ). It is also possible for each task to have its own allocated memory block, with each memory block containing a memory layout consisting of stack, heap, and data.
Below are the advantages and disadvantages of stack Memory: It helps in controlling memory is allocation and deallocation. Stack automatically cleans up the object. Stack memory can not be corrupted easily. Stack memory is minimal.
The stack for a given thread is often contiguous in virtual memory (on Linux and similar systems, and in user mode in Windows). The Windows kernel (in Windows Vista and above) and z/OS allow discontiguous stacks in virtual memory, and GCC 4.6 will also allow that. The compiler does not need to move the stack around at all, even for the systems that have discontiguous virtual addresses for the stack; they just change where new parts are allocated. The operating system might remap physical pages to virtual ones so that the stack may not be contiguous in physical memory, though, even if it is in virtual memory.
There are no requirements for the stack to be contiguous in the language the OS or the hardware.
I challenge anybody to site a reference that explicitly says this is a requirement.
Now a lot of implementations do use contiguous memory because it is simple. This is also how the stack concept is taught to CS students (Stack grows down heap expands up). But there is no requirements to do this. I believe that MS even experimented with placing stack frames in random locations in the heap to prevent attacks the used deliberate stack smashing techniques.
The only requirement of the stack is that frames are linked. Thus allowing the stack to push/pop frames as scopes are entered/left.
The compiler does not try and force the stack to be in contiguous memory. There is no requirements at the language level that require the stack to be contiguous.
If this was the question. Then you would get a more detailed and accurate answer from the community.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With