3 ms·
It seems to me that the most common errors in C/C++ are memory leak and/or corruption issues. Coming from someone who's background is in managed/garbage collec
by Osiris 16y ago
It seems to me that the most common errors in C/C++ are memory leak and/or corruption issues. Coming from someone who's background is in managed/garbage collected languages, doesn't using a managed language help to avoid these common mistakes?
I know that it's still possible to leak memory in C#, like not releasing event handlers, but it's very difficult, if not impossible, to corrupt memory without using unsafe code. In those cases you're probably more likely to be careful with how memory is used or manipulated anyway to avoid causing corruption with the managed code.
- T-R 16y agoMaybe true, but still not much consolation for the embedded systems programmers or maintainers of legacy code.
- agazso 16y agoI would say that the most common errors are because there is no built-in string type (at least in C) and lot of library function expects null terminated strings. And these are hard to get right, because they are very prone to off-by-one errors, and novice programmers tend to write string manipulation routines from scratch, which leads to corruption. Use libraries (e.g. bstring or std::string in C++) that do that for you, and you won't have problems. Regarding memleaks: you can leak database handles, file handles and other resources in managed languages. In C/C++ memory is one of the resources you also take care of. In C++ there are also destructors and you can use RAII to handle most of the cases without thinking.
- zokier 16y agoI wouldn't bundle C and C++ in the same group in memory handling aspects. C++'s destructors and RAII make memory management rather easy (I actually think that it's easier than garbage collection, as I know with some certainty when some memory is allocated and freed. No need to trust some magical black box that GC is.