9

Are there any languages other than C and C++ that can be used explicitly without dynamic memory allocation (i.e. heap) features. In some mission critical embedded systems, use of the heap is prohibited to eliminate memory leak problems for software that may run continuously for many years. Some special purpose compilers also explicitly disable new and malloc to enforce this practice.

I've looked at some of the functional languages, namely Timber and Erlang for their embedded emphasis, but both seem to use heaps with a garbage collector. OCaml and Haskell also use garbage collectors despite static typing, and obviously Python, Ruby, and other dynamically typed languages rely heavily on garbage collection and heap space.

  • Do any high-level languages support this requirement of not dynamically allocating memory?
  • Is this even possible for compilers of functional statically typed languages to do so given their language semantics?
trincot
  • 317,000
  • 35
  • 244
  • 286
kgraney
  • 1,975
  • 16
  • 20
  • 2
    I would consider C++ templates *very* high level (for instance, it's possible to compile [Logic](http://www.mpprogramming.com/cpp/default.aspx) in templates). But it's a lot more difficult than other languages to learn. – CapelliC Jun 08 '13 at 14:15
  • Do you consider [Forth](http://en.wikipedia.org/wiki/Forth_%28programming_language%29) as being a high-level language? – Sylvain Leroux Jun 08 '13 at 18:28
  • I would like to find something more "high-level" than Forth. I am aware that C++ templates [support all kinds of crazy capabilities](http://en.wikipedia.org/wiki/Template_metaprogramming), but I've always felt template metaprogramming is more a discovery hack with the language than an intentional use case. – kgraney Jun 10 '13 at 00:39
  • 1
    **Dynamic allocation** is a bit of a *mis-nomer*. Ie, just banning `malloc` and `new` is not enough. They are just other functions. With *Java* or more specifically *garbage collection*, you can have *un-intentional references*. Something like `alloca()` allows run-time allocation from a *stack* as do C/C++ [Variable length arrays](http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c). Even this may be prohibited as your definition of *dynamic memory* maybe different than others. I believe you want something that pre-determines maximum memory use at compile time. – artless noise Jun 10 '13 at 15:12
  • [FragmentPoolC](http://mail.millennium.berkeley.edu/pipermail/tinyos-2-commits/2011-February/010331.html) and [at U-Utah](http://wiesel.ece.utah.edu/redmine/projects/tinyos-prod/repository/revisions/e95fd8f1fe4394072117d5aec404ca72a454f4b3). Another take relates to time at http://tlsf.baisoku.org/ – artless noise Jun 10 '13 at 15:45

2 Answers2

1

You could have a look at ADA. I've been using ADA83 on embedded platforms a few years ago. It didn't require dynamic allocation at all, and it is as high-level as C is (it's even better than C, in my own opinion). The problem, of course, is to get an ADA compiler for your platform. Maybe GNAT would work for you.

  • From what I've found so far Ada does seem like the best currently available language for this particular requirement. – kgraney Sep 01 '13 at 12:42
-4

A program in essence is data structures and its manipulation by using suitable algorithms. Data has to be held in memory somewhere. It can either be in global, stack or heap memory.

Just because heap is not used is no guarantee that global or stack will not get corrupted by bad code.

If a system is well designed, then it should have all the necessary resources needed, i.e. cpu, memory, os, bandwidth, power, cooling, etc., to perform the desired function.

One can implement by managing global memory instead of heap memory but that would render lot of the libraries that use pointers useless.

I think the best approach is to keep it simple, get lots of dynamic visibility into the system when running/debugging, and make sure that unit tests, code coverage tests and system boundary tests are performed thoroughly before declaring fit for deployment.

If it is well designed, well engineered and well tested then it should do everything well that it is supposed to do and not do anything it is not supposed to do.

There are compiled languages that don't have pointers, e.g. Fortran, but I don't know of any embedded systems that uses Fortran exclusively to implement a system.

Arun Taylor
  • 1,574
  • 8
  • 5
  • 1
    This does not appear to be based on an understanding of the concern in the question, or of the differences between the concepts mentioned. For example, pointers do not imply dynamic allocation. – Chris Stratton Jun 08 '13 at 05:16
  • 2
    in addition, even if the system does _not_ have memory leaks -- heap memory could unexpectedly became exhausted because of heap fragmentation, especially in constrained systems running continuously _for years_... – Sylvain Leroux Jun 08 '13 at 18:26
  • I'm not looking for just a different method of managing memory allocations, but what I am looking for is a language that can compile into purely statically allocated memory with a stack for function calls. So, to Sylvain's point, fragmentation is impossible. The memory should be fully allocated at compile time, with some stack space reserved. – kgraney Jun 08 '13 at 20:24
  • Yes, but beware that if your program is being loaded by an operating system, "allocations" made by the compiler still have to be dynamically realized (as a block) by the operating system. If you are writing a bare-metal program, or one for which whatever kernel you have has made static memory reservation, then realizing whatever the compiler (and linker) have coded is a more deterministic process. – Chris Stratton Jun 10 '13 at 15:11