|
C++ Without Memory Errors(1)
VBar>Home -> C++ Without Memory Errors C++ Without Memory Errorsby Dejan Jelović Here are the slides and the code from a recent talk I've given on writing C++ programs without memory errors. An article with more details will be ready soon.
Memory errors are the worst kind of errors in C and C++. They are hard to reprodUCe and thus hard to debug. Have you ever used a non-trivial C or C++ program that never crashed?
I've been programming in Java for the last year. In Java one sees almost no hard to reproduce problems. Why is that?
Is it because Java is a safer language? No. Java makes you use too many casts and lacks support for types with value semantiCS. It doesn't have destructors so resource management is really hard. It is less safe than C++.
The real reason is garbage collection.
How does garbage collection help? Does it free us from freeing memory? Not really. Try implementing a Vector in Java and you will see how easy it is to forget to set a reference to null, thus leaking memory.
The benefit of garbage collection is this: it guarantees that a pointer will either point to a valid allocated object or be null.
Think about it: if a pointer is either valid or null, there can be no weird memory errors.
How do we do that in C++?
How about adding garbage collection to C++? A company named Geodesic claims their garbage collector for C++ works great.
|