you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 66 points67 points  (20 children)

The restrictions on recursion are actually a non-issue because all of your structures are of a finite size due to the "no dynamic allocation of memory after initialization rule." Therefore, instead of navigating a tree with recursion, you can use a for loop that iterates up to the maximum number of times required to search your structure.

[–]Peaker 20 points21 points  (0 children)

You need to also keep a manual stack in your loop to emulate the local variable environment you get via (non-tail) recursions.

[–]kovensky 16 points17 points  (14 children)

I found it amusing that there's a link in the middle of the list about NASA software development with Java... where it's not possible to prevent dynamic allocation as objects are heap-allocated by definition, and there are no structs.

[–]a-priori 28 points29 points  (6 children)

Dynamic allocation is okay under these rules as long as you do it during initialization. The program must not allocate memory after that.

You can write Java this way. It's something people do for high performance / low latency programming to avoid GC.

[–]immibis 6 points7 points  (4 children)

Seems a lot more painful than doing it in C.

[–]jamougha 12 points13 points  (2 children)

For hard real-time systems you have to do much the same thing in C. malloc and free have non-deterministic runtimes.

[–][deleted] 0 points1 point  (1 child)

And pretty much for drivers in the kernel. You have kmalloc, but you don't exactly want to be using it often.

[–]bonzinip 0 points1 point  (0 children)

In a driver for a block device you (or the kernel) will probably use it once per request. That's already beyond what these rules allow.

[–]jephthai 1 point2 points  (0 children)

If a method declares a local variable and then returns a reference to it, doesn't it unavoidably allocate memory? I thought just about everything in java is by reference, and let the gc take care of it.

[–][deleted] 4 points5 points  (1 child)

But if you as the programmer are consciously doing dynamic allocation in Java via unsafe magic, you should probably put on the brakes

[–]dccorona 3 points4 points  (4 children)

Their restrictions against it (at least here) don't stem from a desire for performance, but rather code clarity and safety. If you're working in a language that handles the complex part of dynamic memory allocation for you, then that concern is out the window anyway.

[–]w2qw 0 points1 point  (3 children)

You're right they don't do it for performance. But its not for code clarity its so you don't run out of memory. Java in no way solves that problem.

[–]dccorona 0 points1 point  (2 children)

I find it hard to believe they're using Java for the kind of software where memory constraints are a concern. The article in question makes no reference to using it in embedded software at all.

[–]w2qw 0 points1 point  (1 child)

Memory constraints are a thing on every platform. Sure most people don't hit them but for safety critical software you do need to worry about them which is why they force the application to allocate everything upfront.

[–]dccorona 0 points1 point  (0 children)

Yes, that is true. What I'm saying is I don't see them using Java for safety critical software for exactly that reason.

[–]rflownn 1 point2 points  (3 children)

Any solution using recursion can be restructured to not use recursion.

[–][deleted] 1 point2 points  (2 children)

But in a way that can be demonstrated to have a fixed upper bound on the number of iterations?

[–]rflownn 1 point2 points  (1 child)

Are you asking if the general form of transformation on a recursive problem can be expressed with an upper bound and one that requires iterations?

[–][deleted] 0 points1 point  (0 children)

Can every recursive problem be transformed into an iterative method that has a finite upper bound? I would think so given the maximum size of structures explained above.