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

2

u/gnosnivek Aug 26 '21

I'm actually not 100% certain how illegal it is. It's UB to call delete on anything not allocated with new, and it's UB to call free on anything not allocated with malloc, but AFAIK there's no rule that says that a pointer allocated with new must have delete called on it (after all, leaking memory isn't UB). So if the implementation of new gets memory from malloc() and doesn't alter it in any way, this might actually be legal on that implementation. I'm not sure (would love a pointer if there's a rule that explicitly makes this undefined!)

That being said, making a habit of this seems like a surefire way to get smacked by horrifying amounts of UB at some point.

Not to mention that the destructors don't get called

Well obviously the solution to that is to manually reimplement the destructor using loops/recursion/free whenever you need to call delete. (We had to do this and it was dumb).

1

u/Ahajha1177 Aug 26 '21

Maybe it's more that it's not cross-platform, as it relies on new calling malloc internally, which seems reasonable but as you said is implementation defined.