Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is stack memory contiguous?

Tags:

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?

like image 588
user2133 Avatar asked Feb 23 '11 03:02

user2133


People also ask

Is the stack contiguous?

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.

Is heap memory contiguous?

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.

What type of memory is stack memory?

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 the contiguous memory?

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 in C++?

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.

Why do we call it stack memory allocation?

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.

Can each task have its own stack memory range?

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.

What are the advantages and disadvantages of stack memory?

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.


2 Answers

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.

like image 65
Jeremiah Willcock Avatar answered Sep 18 '22 23:09

Jeremiah Willcock


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.

But this all orthogonal to the original question.

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.

How is the stack usually implemented.

If this was the question. Then you would get a more detailed and accurate answer from the community.

like image 26
Martin York Avatar answered Sep 20 '22 23:09

Martin York