Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the stack / heap memory model optional?

I can't understand if the use of a stack/heap memory model is a decision for the programmer, or whether it is up to the OS and the programmer has no choice but to work with it.

For example, can stack-less languages like Fortran77 operate across modern platforms still using a stack-less, array based memory model? Or instead, do modern Fortran compilers have to translate the array memory model to a stack/heap memory model? (I can't find much documentation on Fortran memory management.)

If the memory model is a decision for the programmer, why does everything I encounter seem to implicitly assume the stack/heap model is the only option? For example, LLVM operates with stack frames, and I can't find any documentation on managing memory any other way. All languages built on LLVM, even functional languages, must then adopt the stack/heap model when alternative models may be better suited.

If the memory model is a decision for the OS, does this mean writing a program that uses a custom memory model requires writing a custom OS? For example, do I need a custom OS if I want to run a Fortran program that uses the array based memory model Fortran was designed around?

If the answer depends on the OS, please give some comparisons across different OSs.

like image 884
Joseph Johnston Avatar asked Mar 26 '17 05:03

Joseph Johnston


People also ask

Why do we need heap and stack memory?

Stack space is mainly used for storing order of method execution and local variables. Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks. Memory allocated to the heap lives until one of the following events occurs : Program terminated.

Is heap memory better than stack?

Stack memory allocation is considered safer as compared to heap memory allocation because the data stored can only be access by owner thread. Memory allocation and de-allocation is faster as compared to Heap-memory allocation. Stack-memory has less storage space as compared to Heap-memory.

Why heap is used instead of stack?

Stack accesses local variables only while Heap allows you to access variables globally. Stack variables can't be resized whereas Heap variables can be resized. Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any random order.

What is difference between stack memory and heap memory?

Overview. Stack memory is the space allocated for a process where all the function calls, primitive data types (int, double, etc.) and local and reference variables of the functions are stored. On the other hand heap memory is used to store the objects that are created during the execution of a Java program.


2 Answers

Stack and heap have nothing directly to do with Fortran, the standard says nothing about them at all. Similarly C, at least to C89, my knowledge is less good after that. Rather the compiler has to translate the language features as defined by the standard onto an underlying memory model. That memory model is the choice of the compiler implementer, but it is usually most convenient to use whatever features the target OS gives you. Hence you often see stacks and heaps, but at least as far as Fortran and C are concerned that has nothing to do with the programming language.

like image 63
Ian Bush Avatar answered Oct 03 '22 03:10

Ian Bush


It sounds like you have some misconceptions. First of all, FORTRAN implementations generally (always in practice?) use a stack. Classic FORTRAN may not allocate variables on the stack but it has to use the stack to make procedure calls. Even with FORTRAN implementations that use static argument frames, they still create stack frames.

The heap is just memory that is managed as allow random allocations and dealloations of memory. Some programming languages use the heap implicitly, such as to manage dynamic strings and arrays (e.g. BASIC). Other programming languages allow the programmer to use the heap but do not require it (e.g. C). Some programming languages do not generally use the heap at all for programmer accessible constructs (e.g. Cobol, classic FORTRAN).

like image 37
user3344003 Avatar answered Oct 03 '22 01:10

user3344003