Modern C++ is pretty nice if your team is ready to embrace it and leave old styles behind. I still use C# for utilities and prototypes but for production stuff, users' time is more valuable than mine.
Modern C++ is pretty nice if your team is ready to embrace it and leave old styles behind. I still use C# for utilities and prototypes but for production stuff, users' time is more valuable than mine.
Contracts and some of the stuff in the new Core guidelines take it further.
Everyone's needs are different obviously. The stuff I work on has tons of users and start up perf and minimal memory usage are primary requirements. Most of our bugs are nullptr access violations or straight up programming errors that C# doesn't shield you from either.
Not really. RAII still allows for dangling pointers/references. The STL is vulnerable to iterator invalidation. Ranges are likewise vulnerable.
> Contracts and some of the stuff in the new Core guidelines take it further.
How do contracts help memory safety?
The ISO Core C++ bounds/lifetime checker does, yes, but as I mentioned in the other comment I don't know whether it is still going to be C++ in practice.
> The stuff I work on has tons of users and start up perf and minimal memory usage are primary requirements.
Those are valid reasons to use C++, yes.
> Most of our bugs are nullptr access violations or straight up programming errors that C# doesn't shield you from either.
C# doesn't make null dereference undefined behavior :)
2) The invalidation rules are pretty well known at this point, but I concede that it's much better to have compile-time validation. A good STL impl will have iterator debugging, which catches iterator invalidation. e.g: debug mode in GCC
3) Regarding null, you're theoretically right, undefined behaviour and all. In practice[1], accessing a null pointer/reference will result in a crash both in C# and C++.
When developing Java apps (C#'s big brother), the biggest sources of bugs during development were null pointer exceptions and unhandled exceptions propagating to the event loop and killing the UI thread.
[1] There are strange cases such as http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
No, it doesn't.
std::vector<std::unique_ptr<int>> x;
x.push_back(std::make_unique(1));
auto& y = *x[0];
x.clear();
std::cout << y; // use after free
This is, of course, a toy example. For lots and lots of real examples, search Web browser engine bug trackers for UAF vulnerabilities. (They have been using smart pointers exclusively for a decade or so now.)> A good STL impl will have iterator debugging, which catches iterator invalidation.
Valgrind and ASan catch UAF too and have been around for years and years. Yet we still have a ton of UAF vulnerabilities.
In your example, it would be a major red flag in code review to create a reference like that. Some code constructs are just asking for trouble.
I think the reasons behind still having errors despite tools like Valgrind and ASan are: a) many devs don't know about them or use them b) you need 100% coverage to remove all errors
Using said engineering practices will not result in a completely big-free program, but it will have a significant impact on the quality. For something that doesn't have the nightmare security profile of a browser or various other exposed services, that can be just fine, even if not ideal.
I'm guessing the reason why C++ yields lower memory usage than C#/.NET is because RAII and reference counting ensure that memory is always freed as soon as it's no longer needed, whereas garbage collection only ensures that memory belonging to unused objects will be freed sometime later. Is that correct?
Though GC has its own advantages too--the ability to compact being the main one.
*edit: ~16 MB for .Net Native, ~10 MB for C++ using latest VS2015 targeting x86.
Not going to matter for most apps, but for system UI that has to run on multi-user servers it does ;)