News for hackers. Stuff that matters.
Re:Why I Write Games in C (yes, C) by GuB-42 on Saturday September 21, 2019 @06:28PM (#21038277)
I have actually worked on a system where malloc() was forbidden. In fact it always returned null. Buffers were all statically allocated and stack usage was kept to a minimum (it was only a few kB anyways).

The software was shipped with a memory map file so you know exactly what each memory address is used for. A lot of test procedures involved reading and writing at specific memory locations.

It was for avionics BTW. As you may have guessed, it was certified code with hard real time constraints. Exceeding the cycle time is equivalent to a crash, causing the watchdog to trigger a reset.

Re:Why I Write Games in C (yes, C) by Impossible on Saturday September 21, 2019 @09:41PM (#21039095)
Malloc was forbidden in a AAA title I worked on. Interestingly enough we also had two (!!!) embedded garbage collected scripting languages that could only allocate in small (32MB, 16 MB) arenas and would assert if they over allocated.
Re:Why I Write Games in C (yes, C) by zippie on Sunday September 22, 2019 @02:53AM (#21040036)
In modern C libraries malloc is mmap’ed anon pages underneath for an increasingly larger set of conditions these days.
Re:Why I Write Games in C (yes, C) by hackits on Saturday September 21, 2019 @07:27PM (#21038571)
Sounds very similar to NASA C programming guidelines. Each module during the initialization period would allocate its static memory size. Every loop had a upper bound's max iteration to prevent infinite loops.

I think they posted the guidelines and it was a wonderful read about how they developed real time systems.

Re:Why I Write Games in C (yes, C) by mike00632 on Sunday September 22, 2019 @03:05AM (#21040064)
Do you happen to have a link to the guidelines?
Re:Why I Write Games in C (yes, C) by kimown on Sunday September 22, 2019 @04:46AM (#21040339)
Re:Why I Write Games in C (yes, C) by hackits on Sunday September 22, 2019 @04:34AM (#21040306)
Re:Why I Write Games in C (yes, C) by neop1x on Monday September 23, 2019 @03:06AM (#21046579)
I was wondering why I couldn't open this pdf on my android pdf viewer and this link gives me cloudflare's captcha. It uses google's ReCaptcha - storefronts, bicycles, semaphores. What a terrible practice. People stop overusing cloudflare!
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @08:05PM (#21038735)
Why even define a malloc() in this environment if it always returns NULL?
Re:Why I Write Games in C (yes, C) by seanmcdirmid on Saturday September 21, 2019 @09:50PM (#21039130)
Because even if you don't use it directly, you might use some code that uses it. And even then, it might not ever be called given the way you are reusing the code, so...a dynamic check is the best way to ensure it never actually gets used.
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @11:21PM (#21039440)
> Because even if you don't use it directly, you might use some code that uses it.

It seems extremely unlikely that any general purpose code you might adopt, which happens to invoke malloc() at all, would be fit for purpose in such a restricted environment without substantial modification; in which case you would just remove the malloc() calls as well.

> And even then, it might not ever be called given the way you are reusing the code, so...a dynamic check is the best way to ensure it never actually gets used.

In such a restricted environment, it is unlikely you just have unknown deadcode in your project. "Oh, those parts that call malloc()? They're probably not live code and we'll find out via a crash at runtime." That's like the opposite of what you want in a hard realtime system.

So, no — a static, compile/link time check is a strictly superior way to ensure it never gets used.

Re:Why I Write Games in C (yes, C) by lonelappde on Sunday September 22, 2019 @03:40AM (#21040151)
Spoken like someone who never had a 3rd party binary blob as a critical dependency.
Re:Why I Write Games in C (yes, C) by jorams on Sunday September 22, 2019 @04:15AM (#21040260)
If you need your system to never dynamically allocate memory, an opaque 3rd party binary blob which might still do that doesn't seem like a valid inclusion.
Re:Why I Write Games in C (yes, C) by loeg on Sunday September 22, 2019 @07:44PM (#21044739)
Again, we're talking about a very restricted hard realtime environment. How do you trust, let alone qualify, a 3rd party binary blob that you know calls malloc(), which is a direct violation of your own design requirements?
Re:Why I Write Games in C (yes, C) by alxlaz on Sunday September 22, 2019 @05:25AM (#21040438)
Not OP but the most common reason I've seen is that this was an additional restriction that they imposed for their project. The standard library includes a perfectly working malloc. You override it so that you can't end up calling it accidentally, either explicitly (i.e. your brain farts and you do end up malloc()-ing something) or implicitly (i.e by calling a function that ends up calling malloc down the line). The latter is what happens, and surprisingly easy and often. Not all libraries have the kind of documentation that glibc has, nor can you always see the code.
Re:Why I Write Games in C (yes, C) by the_mitsuhiko on Saturday September 21, 2019 @10:11PM (#21039209)
Some things are okay to allocate at some point in time. So you can disable malloc later when you no longer want it to provide memory.
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @11:21PM (#21039443)
That's plausible, but not how OP defined it:

> a system where malloc() was forbidden. In fact it always returned null.

Re:Why I Write Games in C (yes, C) by drblast on Saturday September 21, 2019 @08:14PM (#21038775)
So you crash immediately if someone tries to use it or a library gets added that does?

Never experienced the malloc() thing but do throw exceptions and fail fast under conditions like these so they're caught in testing.

Re:Why I Write Games in C (yes, C) by a_t48 on Saturday September 21, 2019 @09:34PM (#21039076)
I think what he's asking to do is to make it a linker failure by not adding it to the standard lib at all. Compile fails are nicer than runtime fails.
Re:Why I Write Games in C (yes, C) by loeg on Saturday September 21, 2019 @11:23PM (#21039450)
Like sibling commenter says, a compile/link error is even better and faster than finding out at runtime with a crash.
Re:Why I Write Games in C (yes, C) by aetherspawn on Saturday September 21, 2019 @07:26PM (#21038563)
I’d be interested in knowing more if you care to write more about this. I’m currently costing/scoping out what’s required in writing software at that level but don’t really have some real industry insight into what other people do

Static memory is something we already do but we’re pretty interested whether industry actually adopts redundant code paths, monitors, to what extent watchdogs (how many cycles can be missed?), etc.

Re:Why I Write Games in C (yes, C) by rramadass on Saturday September 21, 2019 @11:43PM (#21039532)
I suggest you take a look at the books by Michael Pont and the offerings through his company "SafeTTy Systems". His/Company's work deals with hard real-time systems with all the associated standards.