3 ms·
>Can you write code that is completely safe? Yes. Just don't use raw pointers. Use unique_ptr, shared_ptr and weak_ptr instead.
by tzahola 8y ago
>Can you write code that is completely safe?
Yes. Just don't use raw pointers. Use unique_ptr, shared_ptr and weak_ptr instead.
- ant6n 8y agoAlso, keep things on the stack (C++11 onward has features that allow avoiding copies).
- sclangdon 8y agoThese things don't make it safe in the security sense. They just help against memory leaks.
- beojan 8y agoI don't think you can ever write code that's completely safe then, if nothing else some processor flaw will come along and ruin everything.
- dbaupp 8y agoWhile it's true that processor flaws destroy the assumptions that higher level components (such as any/all programming languages) build on, you don't need to go nearly that far to see unsafety in C++, even using only the most modern techniques: use-after-move of many types is undefined behaviour (for instance, dereferencing a std::unique_ptr that has been moved from), and iterator invalidation & dangling references aren't addressed by those smart pointers at all.
- xwvvvvwx 8y agosmart pointers are a good start, but completely safe is a pretty big exaggeration. Just off the top of my head smart pointers do not protect against: - null pointer dereferences - out of bounds array access - iterator invalidation - dangling (non-owning) references
- pjmlp 8y agoNo, but C++ compilers like Visual Studio do. https://blogs.msdn.microsoft.com/vcblog/2017/08/11/c-core-guidelines-checker-in-visual-studio-2017/ https://blogs.msdn.microsoft.com/vcblog/2017/08/11/c-core-gu... https://blogs.msdn.microsoft.com/vcblog/2017/06/28/security-features-in-microsoft-visual-c/ https://blogs.msdn.microsoft.com/vcblog/2017/06/28/security-... https://docs.microsoft.com/en-us/cpp/security/security-best-practices-for-cpp https://docs.microsoft.com/en-us/cpp/security/security-best-...