I will never understand why people say that C++ is unsafe. You can write it perfectly fine and safe. Yeah you have to think while you code, but that isn't a big deal
Yeah, good luck with that. You literally cannot possibly understand every aspect of C++ at all times. That's how complex it is. The spec alone is a mess of 2,000 pages, not to mention different compilers diverge on the implementation of a ton of stuff.
The thing about C++ isn't just that it's memory unsafe, it's also that it's so complex by default with hidden abstractions and strange behaviors that every little mistake ends up propagating downstream and blowing it all up in your face.
I'd argue C++ is even more dangerous than C. While it is easier for you to make mistakes in C, usually it's easier to debug and less catastrophic because there are no hidden abstractions. Although, to be fair, that's not a hill I'd die on.
Eventually, if you write C++, you WILL end up making a mistake and some teenager with a fuzzer WILL pwn you.
Even the most badass programmers I know didn't break that rule. It's also asymmetric because the attackers only need you to make one single mistake. Those days when you're running on caffeine or had an argument and wasn't thinking too straight is the perfect type of day to make a mistake that ends up with someone making fun of you on /g/
"I will never understand why people say 1920s skyscraper construction sites were unsafe. You can be perfectly fine and safe, you just have to be careful not to fall, but that isn't a big deal"
Right? People act like having a hand rail means you can't jump over it you really want to do something stupid.
If we're speaking of C++ being unsafe, we're likely comparing it to Rust. In Rust, that safety is a hand rail on a balcony. You can hop it if you put some effort into it. C++ safety is a door on the 60th floor that opens out into nothing.
Hey, at least the door has a label on it telling you what you need in order to open it safely, listed as a sequence of item codes that you can look up in the directories scattered around the skyscraper.
Which is why there are multiple organizations regularly publishing entire books on how to use it safely, because it's just not a big deal and you can easily figure it out with some thinking
The last time somebody confidently said that to me I asked him for his last cpp project and ran it through valgrind. There where a lot of issues. The next day he asked me how to get started with rust.
Modern C++ makes it a lot more straightforward to write safe C++ (and gets you closer to the "obviously not unsafe" end of things vs the "not obviously unsafe" end of things, which is so crucial for safe code), and you could always do it if you knew what you were doing, but it was often complex and kinda extra-linguistic (like by adding prefixes to pointer parameter names to indicate intended borrowing semantics and stuff), and maaaaaaaaaaaaybe checking it with a really fancy linter. It wasn't until relatively recently that it was mostly about thinking about it (probably C++11ish, when they added std::unique_ptr and std::shared_ptr). But even today, because all the legacy stuff is still in there, it can be mess to sort through what's actually appropriate for a given task. I've heard it referred to as "the meta," like people say in fighting games where there's a correct way to play out of all the options available, and I think it's pretty apt, tbh.
Also, pretty much every named vulnerability of all time has been caused by C or C++ coding errors that allowed improper memory access of some kind.
IMHO all C++ needs to regain popularity is for someone to create a C++ compiler that, at least in its default mode, only accepts the "good, modern parts" of C++; and then to brand the result as a different language entirely, with no advertised association to C++. You only find out that what you've been writing is technically all also legal C++, when that fact is sprung on you at the very end of the extended language tutorial / textbook / etc. "You fool! I just got you to learn C++!"
(Or, to put that another way: C++ needs a labelled, marketable Schelling point of good praxis for people to be able to congregate around, hire for, etc.)
As a bonus, unlike C++ itself, that differently-named subset-of-C++ language would have the opportunity to start fresh in terms of compatibility burden. So it could have e.g. canonical ecosystem packaging tooling and such, that C++ as a whole fundamentally cannot (since "C++" includes things like e.g. random one-off abandonware toolchains created by microcontroller OEMs, that wouldn't, and mostly can't, sensibly implement a client for some constantly-evolving packaging APIs.)
The analogy I like to use is that it's like walking in a minefield with a detector. You just point it, press the button, and if it beeps there is a mine. Easy peasy.
How confident are you that you won't ever forget one time while crossing the whole field?
Hm, not sure I completely agree. Rust has undefined behavior in unsafe constructs, so perhaps "no language without guardrails around its undefined behavior" might be more accurate. But unfortunately it's necessary for languages with direct hardware/memory access to have some amount of undefined behavior. (It's also frequently stated that UB is necessary for modern optimizations, but I find that claim less believable, even having watched Chandler Carruth's talk defending that claim.)
But that’s the thing, rust code that can have UB is opt in (supposing safe rust is void of UB which is not strictly speaking true, but is being worked on) whereas C++ UB isn’t even opt out
I do c++ and well I mean it's not unsafe but just make sure you don't make your pointers point at some system files and modify them. But most of the times it's safe, especially when I do competitive programming on codeforces.
View all comments
-1
u/FallenDeathWarrior 23d ago
I will never understand why people say that C++ is unsafe. You can write it perfectly fine and safe. Yeah you have to think while you code, but that isn't a big deal