logoalt Hacker News

The C++ standard for the F-35 Fighter Jet [video]

264 pointsby AareyBabayesterday at 6:07 PM280 commentsview on HN

PDF: https://www.stroustrup.com/JSF-AV-rules.pdf


Comments

bri3dyesterday at 10:19 PM

https://web.archive.org/web/20111219004314/http://journal.th... (referenced, at least tangentially, in the video) is a piece from the engineering lead which does a great job discussing Why C++. The short summary is "they couldn't find enough people to write Ada, and even if they could, they also couldn't find enough Ada middleware and toolchain."

I actually think Ada would be an easier sell today than it was back then. It seems to me that the software field overall has become more open to a wider variety of languages and concepts, and knowing Ada wouldn't be perceived as widely as career pidgeonholing today. Plus, Ada is having a bit of a resurgence with stuff like NVidia picking SPARK.

show 2 replies
anonymousiamyesterday at 8:15 PM

The same is true for the software that runs many satellites. Use of the STL is prohibited.

The main issue is mission assurance. Using the stack or the heap means your variables aren't always at the same memory address. This can be bad if a particular memory cell has failed. If every variable has a fixed address, and one of those addresses goes bad, a patch can be loaded to move that address and the mission can continue.

show 4 replies
don-codeyesterday at 11:11 PM

> All if, else if constructs will contain either a final else clause or a comment indicating why a final else clause is not necessary.

I actually do this as well, but in addition I log out a message like, "value was neither found nor not found. This should never happen."

This is incredibly useful for debugging. When code is running at scale, nonzero probability events happen all the time, and being able to immediately understand what happened - even if I don't understand why - has been very valuable to me.

show 2 replies
jandrewrogersyesterday at 6:35 PM

For those interested, the F-35 (née Joint Strike Fighter) C++ coding standards can be found here, all 142 pages of it:

https://www.stroustrup.com/JSF-AV-rules.pdf

show 8 replies
xvilkatoday at 8:22 AM

Time to rewrite it in Rust.

time4teayesterday at 7:31 PM

a = a; // misra

Actual code i have seen with my own eyes. (Not in F-35 code)

Its a way to avoid removing an unused parameter from a method. Unused parameters are disallowed, but this is fine?

I am sceptical that these coding standards make for good code!

show 10 replies
geophphyesterday at 7:35 PM

LaurieWired is an awesome follow on YouTube!

show 1 reply
barfoureyesterday at 6:29 PM

Do avionics in general subscribe to MISRA C/C++ or do they go even further with an additional (or different) approach?

show 3 replies
djfobbztoday at 12:00 AM

I wonder if Lockheed Martin has an Electron based future fighter in the works?

kalugayesterday at 7:29 PM

The “90% ban” isn’t about hating C++ — it’s about guaranteeing determinism. In avionics, anything that can hide allocations, add unpredictable control flow, or complicate WCET analysis gets removed. Once you operate under those constraints, every language shrinks to a tiny, fully-auditable subset anyway.

greenavocadotoday at 12:27 AM

The C++ standard for the F-35 fighter jet prohibits ninety percent of C++ features because what they are actually after is C with destructors. I was just thinking about how to write C in a modern way today and discovered GLib has an enormous about of useful C++ convieniences in plain C.

Reading through the JSF++ coding standards I see they ban exceptions, ban the standard template library, ban multiple inheritance, ban dynamic casts, and essentially strip C++ down to bare metal with one crucial feature remaining: automatic destructors through RAII. When a variable goes out of scope, cleanup happens. That is the entire value proposition they are extracting from C++, and it made me wonder if C could achieve the same thing without dragging along the C++ compiler and all its complexity.

GLib is a utility library that extends C with better string handling, data structures, and portable system abstractions, but buried within it is a remarkably elegant solution to automatic resource management that leverages a GCC and Clang extension called the cleanup attribute. This attribute allows you to tag a variable with a function that gets called automatically when that variable goes out of scope, which is essentially what C++ destructors do but without the overhead of classes and virtual tables.

The heart of GLib's memory management system starts with two simple macros: g_autofree and g_autoptr. The g_autofree macro is deceptively simple. You declare a pointer with this attribute and when the pointer goes out of scope, g_free is automatically called on it. No manual memory management, no remembering to free at every return path, no cleanup sections with goto statements. The pointer is freed whether you return normally, return early due to an error, or even if somehow the code takes an unexpected path. This alone eliminates the majority of memory leaks in typical C programs because most memory management is just malloc and free, or in GLib's case, g_malloc and g_free.

The g_autoptr macro is more sophisticated. While g_autofree works for simple pointers to memory, g_autoptr handles complex types that need custom cleanup functions. A file handle needs fclose, a database connection needs a close function, a custom structure might need multiple cleanup steps. The g_autoptr macro takes a type name and automatically calls the appropriate cleanup function registered for that type. This is where GLib shows its maturity because the library has already registered cleanup functions for all its own types. GError structures are freed correctly, GFile objects are unreferenced, GInputStream objects are closed and released. Everything just works.

Behind these macros is something called G_DEFINE_AUTOPTR_CLEANUP_FUNC, which is how you teach GLib about your own types. You write a cleanup function that knows how to properly destroy your structure, then you invoke this macro with your type name and cleanup function, and from that moment forward you can use g_autoptr with your type. The macro generates the necessary glue code that connects the cleanup attribute to your function, handling all the pointer indirection correctly. This is critical because the cleanup attribute passes a pointer to your variable, not the variable itself, which means for a pointer variable it passes a double pointer, and getting this wrong leads to crashes or memory corruption.

The third member of this is g_auto, which handles stack-allocated types. Some GLib types like GString are meant to live on the stack but still need cleanup. A GString internally allocates memory for its buffer even though the GString structure itself is on the stack. The g_auto macro ensures that when the structure goes out of scope, its cleanup function runs to free the internal allocations. Heap pointers, complex objects, and stack structures all get automatic cleanup.

What's interesting about this system is how it composes. You can have a function that opens a file, allocates several buffers, creates error objects, and builds complex data structures, and you can simply declare each resource with the appropriate auto macro. If any operation fails and you return early, every resource declared up to that point is automatically cleaned up in reverse order of declaration. This is identical to C++ destructors running in reverse order of construction, but you are writing pure C code that works with any GCC or Clang compiler from the past fifteen years.

The foundation beneath all this is GLib's memory allocation functions. The library provides g_malloc, g_new, g_realloc and friends which are drop-in replacements for the standard C allocation functions. These functions have better error handling because g_malloc never returns NULL. If allocation fails, the program aborts with a clear error message. This might sound extreme but for most applications it is actually the right behavior. When malloc returns NULL in traditional C code, most programmers either do not check it, check it incorrectly, or check it but then do not have a reasonable recovery path anyway. GLib acknowledges this reality and makes the contract explicit: if you cannot allocate memory, the program terminates cleanly rather than stumbling forward into undefined behavior.

show 1 reply
factorialboyyesterday at 10:21 PM

Isn't the F35 program considered a failure? Or am I confusing it with some other program?

show 8 replies
flamedogetoday at 6:26 AM

surprised not that strict about types. i remember google doesn't allow unsigned?

thenobstayesterday at 7:14 PM

I wonder how these compare to high frequency training standards. It seems like they'd have similar speed/reliability/predictability requirements in the critical paths.

show 2 replies
dzongayesterday at 8:26 PM

I guess a bigger conversation could be had in regards to:

what leads to better code in terms of understandability & preventing errors

Exceptions (what almost every language does) or Error codes (like Golang)

are there folks here that choose to use error codes and forgo Exceptions completely ?

show 1 reply
rramadasstoday at 5:40 AM

AUTOSAR's free pdf file Guidelines for the use of the C++14 language in critical and safety-related systems (defined as an update to MISRA C++ 2008) - http://www.autosar.org/fileadmin/standards/R18-10_R4.4.0_R1....

Note that both MISRA and AUTOSAR's guidelines have been combined into a single standard "MISRA C++ 2023" which has been updated for C++17.

Breaking Down the AUTOSAR C++14 Coding Guidelines - https://www.parasoft.com/blog/breaking-down-the-autosar-c14-...

zerofor_conducttoday at 2:25 AM

program like a fighter pilot? that makes no sense.

manoDevyesterday at 7:41 PM

You mean fighters ARE coded in C++? My god

show 2 replies
FpUseryesterday at 10:47 PM

Her point about exceptions vs error codes was that one failed to catch exception of particular and and things went south meanwhile if we instead "catch" error code all will be nice and dandy. Well one might fail to handle error codes just as well.

That is of course not to say that exceptions and error codes are the same.

nikanjyesterday at 7:11 PM

In 1994 C++ compilers were buggy, and a modernization of the C++ allowed features list is still stuck in a committee somewhere?

ltbarcly3yesterday at 7:22 PM

Paging our Ada fanboys! You're missing it!

show 1 reply
mwkaufmayesterday at 6:15 PM

TL;DR

- no exceptions

- no recursion

- no malloc()/free() in the inner-loop

show 9 replies
xinem10yesterday at 7:28 PM

[dead]

bigyabaiyesterday at 6:22 PM

[flagged]

show 1 reply
semiinfinitelyyesterday at 7:22 PM

even with 90% of c++ features banned, the language remains ~5x larger than every other programming language

show 1 reply
chairmansteveyesterday at 10:55 PM

Ahhh. They use C++.....

That explains all the delays on the F-35....,

show 2 replies
grougnaxtoday at 8:45 AM

They should use Rust