Using the STL after using Java collections or Qt collections is a shock. I think there really needs to be a "Safe STL" in addition to "Performant STL" that performs range checks. You simply cannot recommend using the STL for safe code.
SaferCPlusPlus[1] provides compatible, memory-safe versions of the most commonly used STL containers. (The compiler switches mentioned by others add range checking, but do not, for example, catch use-after-free bugs.)
> > It's not simple to write memory safe code in C++, otherwise we would have a lot fewer vulnerabilities in C++ codebases.
Using SaferCPlusPlus, it now is simple to write memory-safe code in C++. And it's not much different from writing traditional (unsafe) C++ code.
[1] shameless plug: https://github.com/duneroadrunner/SaferCPlusPlus
indeed. for gcc, you can compile stuff with '-D_GLIBCXX_DEBUG' to enable random-iterator bound checks. moreover, random-access-iterators do have the 'at(...)' stuff available for bounds checked access to the containers.
but, yeah, a standardized way would be very useful.
It is also possible to explicitly enable them in release code.
https://blogs.msdn.microsoft.com/vcblog/2017/06/28/security-...
Why?
I prefer having a free-standing generic find function as opposed to it being implemented in each and every container. Even better example: copy. You can use it with containers, but also with input/output streams (using stream iterators).
You mentioned safety, is iterator invalidation bothering you?
Most popular languages don't allow putting objects on the stack and thus have at least comparable overhead anyway.
If you are sensitive to these overhead, you pretty much already only have C++ as your only choice. Or maybe Rust, but I do not know rust well to comment if Rust does not have such overhead.
> It's not simple to write memory safe code in C++
No, in C++11 or newer, memory safe code is the default. That's the design goal of the newer standards of C++. IMHO, memory safety is no longer a central issue for C++.
> a lot fewer vulnerabilities in C++ codebases.
This only applies to legacy code.
If you want dynamic dispatch, you don't use a virtual class, you use a "trait object". Basically it's a double pointer: a (pointer to vtable, pointer to data). Technically the C++ standard (as far as I know) doesn't define the exact implementation of this stuff, but in my understanding the vtable pointer is stored with the data.
There are pros and cons to each approach.
Is this referring to Rust? Then it sounds like it will have similar overhead as C++'s virtual functions.
I have been preaching how to write proper C++ code vs "nicer C" since the late 90's, but without much success, specially to enterprise developers.
Many of the so called modern C++ features were already possible with C++98.
As a good example, MFC 1.0 could have been like Turbo Vision, OWL, VCL, Qt and so on, but the test group from Microsoft found Afx too high level and wanted just a thin layer over Win16 thus it was reborn as MFC.
> I have been preaching how to write proper C++ code vs "nicer C" since the late 90's, but without much success, specially to enterprise developers.
In no practical sense is it easy then. Even if you find it easy to write perfect code, you still have to work with other people that don't and use libraries which have problems.
C++ has so many ways to shoot yourself in the foot I literally cannot think of a mainstream language that is actually more difficult to master. Would you say it was easy compared to Python, Java or JavaScript to write safe code in? Those languages have no undefined behaviour and they all have automatic memory management compared to C++.
However, the mechanisms for safer code are there, if one cares to use them in first place.
The usual set warnings as errors, CI build with static analyzers that break on failure, unit tests with sanitizers that break on failure.
But many companies don't care to set this workflow.
They also don't care for doing that for Python, Java or JavaScript as well.
JavaScript might have automatic memory management, but it has its own collection of WTF moments and implementation issues across browsers.
Java is way better, yet there is a reason why Java Puzzles are so entertaining.
While a tracing GC is much more productive than RAII/smart pointers, sometimes you also need to track down those references that prevent the collector to do its job.
Android developers have lots of "fun" doing that regarding context and activities.
One should not forget that safe languages only get us free from memory corruption, regarding safe code. There is still the whole spectrum of logical errors, specially bad in dynamic languages.
In any case, until we finally get a mainstream OS written in Swift, .NET Native, Rust or D, we need to make the best use of our tooling for low level system coding activities and in this regard C++ is so much better than C.
Because as much as I would like to use a safer alternative like Swift, D or Rust, as an ex-Pascal/Oberon user, they aren't there yet.
There isn't much Ada tooling available for those environments, and even using GNAT would be increasing project costs regarding having to implement ourselves stuff that we get for free with the SDK tools that support C++ out of the box.
exactly! C++11 only made it more pleasant to write.
Are you sure you're not referring to _C_ codebases?
It's not simple to write memory safe code in C++, otherwise we would have a lot fewer vulnerabilities in C++ codebases.