Hacker News new | past | comments | ask | show | jobs | submit login

>Can you write code that is completely safe?

Yes. Just don't use raw pointers. Use unique_ptr, shared_ptr and weak_ptr instead.




smart 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



Also, keep things on the stack (C++11 onward has features that allow avoiding copies).


These things don't make it safe in the security sense. They just help against memory leaks.


I 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.


While 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: