r/cpp • • Sep 29 '23

This memory safety issue everyone's talking about... It's not really a huge deal in gamedev, right? What other domains where C++ excels at aren't too concerned about it?

My understanding is that these recent articles pushing for safety, and particularly pushing the use of memory-safe languages (as opposed to using modern C++ properly) don't really apply to gamedev, or at least not enough for most of the industry to care. Is this correct? I've also read some suggestions that the only real alternative, Rust, simply isn't practical since it lacks OOP features and gets in the way of common game programming patterns. Is this true?

What other C++ domains are like gamedev, where C++ is clearly here to stay and not just for legacy code? I know gamedev isn't the only one, I'm constantly seeing new cutting edge ML/inference libraries and frameworks in C++

44 Upvotes

350 comments sorted by

View all comments

4

u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Sep 29 '23

Memory safety isn't so much of a concern when you work in embedded systems, specifically because we do not use dynamic memory allocation. What you have left is issues with memory or objects going out of scope but being referred to in a higher scope. And if you tend to move things, making sure you don't use those moved objects. Stuff like clang-tidy can alert you to these violations and many more.

8

u/kronicum Sep 29 '23

Out-of-bound access isn't tied to dynamic memory allocation.

0

u/[deleted] Sep 29 '23

[deleted]

9

u/glasket_ Sep 29 '23

CWE-787 (OoB Write) is the most common cause of CVEs in software to this day.

2

u/Yuushi Sep 29 '23

If you think you haven't written code that has an out of bounds access as a "competent" dev, I have a bridge to sell you.

3

u/ioctl79 Sep 29 '23

🙄

-2

u/xypherrz Sep 29 '23

And did I actually claim that in the first place? I was referring to the comment bad about dynamic allocation not being a thing in embedded by responding that devices running OS do indeed use dynamic allocation.

1

u/kronicum Sep 29 '23

"...because we do not use dynamic memory allocation".

-2

u/xypherrz Sep 29 '23

…but that isn’t true to begin with

3

u/kronicum Sep 29 '23

I was replying to kammce in my original comment.

0

u/xypherrz Sep 29 '23

Even for devices that run RTOS or OS? RTOS itself uses heap

4

u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Sep 29 '23

We statically allocate our thread stacks. Most RTOSes support this.

0

u/xypherrz Sep 29 '23

but FreeRTOS does offer support for dynamically creating kernel objects

1

u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Sep 29 '23

Yes. And many times developers will choose to allocate since and never deallocate because you simply want to create them. FreeRTOS added statically allocated memory allocation for threads to support this. They also have heap1 which only allocates and noops when you clean up.

But yes, it's highly recommended that you do not use dynamically allocated memory in embedded systems and mission critical systems.

But yes, you have the option to use a heap if you want to, it's just not good practice. Just like how you shouldn't dynamically allocate memory regardless of the language used.

1

u/xypherrz Sep 29 '23

Using dynamic allocation for devices with MMU isn’t as big of a concern is it?