3 ms·
>Can you write code that is completely safe? No. The modern C++ features like std::unique_ptr and std::shared_ptr replace old patterns of "malloc()/free()" an
by jasode 8y ago
>Can you write code that is completely safe?
No. The modern C++ features like std::unique_ptr and std::shared_ptr replace old patterns of "malloc()/free()" and "new/delete". This ensures correct cleanup and avoids leaking memory.
However, there are other topics of "memory safety" such as preventing runtime behavior of buffer overruns and writing to invalid pointers. The virtual machines like Java JVM and C# CLR create a constrained memory area with extra runtime checks and the new C++ language features don't replicate that type of memory safety. Also, those "managed" languages don't expose raw pointers as a 1st class programming feature (e.g. don't use "unsafe" blocks) so that in itself creates an environment of extra memory safety.
- zelos 8y ago"This ensures correct cleanup and avoids leaking memory." Although shared_ptr does open you up to a whole new class of bugs, with circular references causing memory leaks and object lifetimes being harder to reason about, especially in a multi-threaded environment. I agree in general, though, things are a lot better.