Is it possible that a general-purpose programming language does not have a heap?

I am creating a programming language. What interests me, in a language that contains a reference construct, is it possible to have a new / malloc operator? That is, all variables are stored either on the stack somewhere or are statically distributed.

The idea is that you get better type safety as well as "free garbage collection" without actually having a garbage collector.

I am not familiar with too many scripting languages, so if you are already doing this, feel free to point it out.

(Dynamic / unknown size data structures will be processed by the dynamic list structure, which will be processed (obviously) on the heap behind the user's back.)

+4
source share
4 answers

Fortran has always been a general-purpose language, but it did not support any dynamic memory allocation out of the box.

A common practice was to distribute a large array statically and simulate native memory management on top of it.

If you manage to get rid of the GC and manual memory management, then analyzing the area can help, but only in a few specific cases.

+2
source

Managing memory at the regional level is one approach to not having a heap managed in the traditional sense. This is manifested in languages ​​such as FX and MLKit .

+1
source

You can select objects on the stack if you know that after the method is completed they will not be referenced. This means that the object is used exclusively using a method (for example, a temporary object) or is used exclusively in a method called by nested calls. This, however, is a serious limitation. There are several dynamic optimizations that go in this direction (at least optimization of a temporary object). Perhaps you may have a static checking mechanism that applies this restriction or perhaps distinguishes between heaps and stack objects with types ...

0
source

There is no need at all that you absolutely need to implement a stack or a bunch. C does not indicate, for example, a stack. In fact, in many languages ​​you don’t even have to care, you just indicate that the implementation (compiler, interpretation, or something else) frees up space for the variable, and possibly for a long time.

Your language translator (presumably one) could do int main(void) { char memory[1048576]; run_script_from_stdin_using(memory); } int main(void) { char memory[1048576]; run_script_from_stdin_using(memory); } int main(void) { char memory[1048576]; run_script_from_stdin_using(memory); } . You can even call mmap (2) to get an anonymous block of memory, and use this to insert your variables. It just doesn't matter where the objects live, and this stack / heap are terms that are of dubious value, given that they are often interchangeable.

-1
source

Source: https://habr.com/ru/post/1386592/


All Articles