r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.

204 Upvotes

327 comments sorted by

View all comments

Show parent comments

7

u/[deleted] Aug 25 '21

(things like unique_ptr/shared_ptr can only reduce dangling pointers, not eliminate them)

I disagree strongly. With a tiny bit of discipline that becomes automatic, you can completely prevent dangling pointers, every single time.

Knowing their might be a memory corruption issue hiding somewhere is no fun.

Again, you can almost mechanically prevent this from happening by following best practices. If you move back and forth between C and C++, this of course increases the risk, but if you write wrappers to deal with each API, you can guarantee such issues just won't happen.

2

u/pedersenk Aug 25 '21 edited Aug 25 '21

In principle, possibly (though I am not convinced). I have an extensive test suite from work filled with fairly decent evidence that mistakes do indeed happen with C++ (in particular the STL).

Not to mention that the very existence of AddressSanitizer and Microsoft's safe iterators (neither of them pick up 100%) means that there is lots of room for improvement.

Actually, iterators themselves are barely better than iterating raw pointers. How can you honestly say these are safe? (Other than, try not to make mistakes ;))

3

u/[deleted] Aug 25 '21

In practise, I barely use iterators directly. Range based for loops and algorithms see to that. The only time I really use them is as the result of a find() != end(container) type situation, and that's hardly a gotcha situation.

0

u/pedersenk Aug 25 '21 edited Aug 25 '21

The range based loops do protect from most mistakes from wrangling iterators directly (this is good. I use them heavily at work!) but it is still very difficult to verify systematically that a function called somewhere further down the call stack will not invalidate the vector whilst going through it (vector<T> is mainly the issue but even list<T> can get invalidated in some rare cases). Also removing whilst iterating needs special care (often it is safer to avoid iterators completely for that part).

One I think is quite fun to demonstrate (and not all can agree on this within a couple of mins when I present it to them in interviews)

std::vector<uint32_t> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.emplace_back(v.at(1)); // Valid?
v.push_back(v.at(1)); // Valid?

Some discussion: here and here.

The standard has made a special case for allowing this (or it would be reading from invalidated memory just after the vector resizes). However when a vector is wrapped within layers of other peoples programs, they rarely provide the same special treatment. This kind of extra cognitive work thinking about it makes hobby personal projects with the standard C++ STL "un-fun".

Microsoft's safe iterators (unfortunately unavailable on platforms I use) obviously do some locking (as does my custom STL). In my opinion, more things should be locked (in the debug builds).

In my STL I do locking on '->' to alert me of dangling 'this'. I also lock (and bounds check) on array / vector[index]. Finally I also lock the lifespan of references received from string[idx] and vector[idx].

It results in a ~20% slowdown in debug builds. It can probably be optimized but that is fairly unnecessary. Release builds are generally all I care about for performance.

3

u/[deleted] Aug 25 '21

I dunno man, I feel like dangling pointers were a problem for me back in 2003, but they are just something that don't happen to me anymore, because of the way I naturally write C++. I don't do any funky stuff, and use RAII a lot.

3

u/pedersenk Aug 25 '21

Don't get me wrong, it very rarely happens (mainly since C++0x onwards). But the fact I can't guarantee it can never happen does ruin the fun a little for me. So that is why as a hobby I looked at resolving that.

Very likely OCD more than anything else ;)

1

u/[deleted] Aug 25 '21

Hehe fair enough :)

2

u/[deleted] Aug 27 '21

Your point about iterators is good: have an upvote!

the very existence of AddressSanitizer and Microsoft's safe iterators

I came up with another hard-to-avoid way to get a dangling pointer, which is to take a reference to an entry in a std::vector, and then the vector gets resized and the new allocation is somewhere completely different in memory.

So maybe I'm too optimistic.

The last eight or so years I was doing C++ full-time, I just stopped running into such problems entirely, but I'm now realizing it's a combination of smart pointers and bitter experience.

1

u/pedersenk Aug 27 '21

take a reference to an entry in a std::vector, and then the vector gets resized and the new allocation is somewhere completely different in memory.

Absolutely. Both bounds checked .at(idx) and operator[idx] return a reference so you just have to make sure the lifespan of that reference is shorter than potential invalidation. But I suppose we are paid to get this right during work hours ;).

However as a hobby, I like to "dumb down" and know that if I get it wrong, I get a slap rather than go on the hunt for potentially tricky UB.

As it stands my "safe" STL manages to lock this reference lifetime in quite a janky way. It takes an object as a parameter (implicitly converted from a size_t). That way if the vector invalidates whilst that object exists, it will abort with an error immediately. So yeah janky as hell but I honestly believe C++ to have a better lifespan than "safe" alternatives like Rust+Bindings so it is worth the discomfort!

0

u/Full-Spectral Aug 26 '21

Not really. It's quite easy to create problems using smart pointers, they are just different types of problems. A good analyzer can catch a lot of them, but not all.