5 ms·
I’ve been writing C++ code since it’s inception in the 1980’s. In the last two weeks I’ve written close to 1000 lines and no raw ptrs… in fact no smart ptrs eit
by waynecochran 4y ago
I’ve been writing C++ code since it’s inception in the 1980’s. In the last two weeks I’ve written close to 1000 lines and no raw ptrs… in fact no smart ptrs either… not even one c-style array. No worries about leaks or memory safety. It doesn’t look anything like C++ code I wrote even 10 years ago.
- Icathian 4y agoI'm curious and would love to take a look. Any chance any of that code is available somewhere? My c++, little that I write, mostly looks like C and I'd love to learn something new.
- waynecochran 4y agoMost of its in a private GitHub repo but I think I can open a piece of it… here is part of it ... a header only library for computing closest pairs... https://github.com/wcochran/closest-pairs/blob/main/closest-pairs.h https://github.com/wcochran/closest-pairs/blob/main/closest-...
- mike_hock 4y agoWhy is Eigen/Dense included if you never refer to anything Eigen and just take it as a duck-typed template parameter?
- waynecochran 4y agoTrue.. good eye .. the operations on type P are very Eigen specific, but another type could easily provide those.
- bugfix-66 4y agoAfter giving your code a quick skim, I cannot see any mistakes in your vector indexing. However, your code is full of vector accesses without bounds checking: vec[i] instead of vec.at(i). That's just as "unsafe" as C, so I recommend not bragging about how far you've come. Your code is full of what is essentially unprotected C-style *(addr + i) = ... ... = *(addr + i) You've been writing C++ since the 1980's, but your code is full of raw pointer math, and yet... "No worries about memory safety"? I have a feeling you'll be reaching for Valgrind or address sanitizer someday soon, just like the C programmers.
- waynecochran 4y agoNot bragging at all.. it was just introspection on how different my code looks now… maybe I should find some older code that is fraught was dangerous constructs. In fact, early versions of this were segfaulting for the very reasons you mention.
- bugfix-66 4y agoThank you for being honest. This safe, "no worries" C++ was segfaulting. You've managed to make it correct today but it's still unsafe. Someday one of your coworkers is going to break it and this code will be trashing the heap. Mysterious memory corruption failures are your destiny. That said, Happy Halloween!
- WoodenChair 4y agoYour comments come across as pretty mean-spirited. Let me rewrite one line for you. You wrote: > Someday one of your coworkers is going to break it and this code will be trashing the heap. Mysterious memory corruption failures are your destiny. You could have written something like, "It's possible this code will break in the future and you won't even realize the cause, because it will appear as a mysterious memory corruption failure." See how I left out a word like "destiny" and took out the whole bit about coworkers? Yeah, much nicer and less personalized.
- bugfix-66 4y agoAnd then I said "Happy Halloween". The point was that being a C++ programmer is like being part of a horror movie. I say that as a person who uses C++ in my day job. It's a nightmare.
- waynecochran 4y agoI have a love/hate relationship with C++ ... probably like most C++ programers.
- Icathian 4y agoMuch appreciated, thank you!
- hyperhopper 4y agoMost Google C++ code is written like that, check out their repos on GitHub
- throwaway311022 4y ago
- brnt 4y agoA project that my team works on might give you an impression: https://github.com/arbor-sim/arbor https://github.com/arbor-sim/arbor
- waynecochran 4y agoThat is some hard core science simulation...
- deltasevennine 4y agoHe's just talking about using std::vector everywhere.
- waynecochran 4y agoThat and several other std types + algorithms…
- swagonomixxx 4y agoBut std::vector has been around since C++98 (and in the STL even before then). What's changed?
- waynecochran 4y agoA lot. Reading Scott Meyer’s Effective Modern C++ makes me realize how different things are. Just providing a type doesn’t do it… it’s the whole ecosystem around it.
- zmmmmm 4y agoit was such a PITA to use in 1998 ...
- galangalalgol 4y agoIf you consider thread safety an aspect of memory safety, I still see those issues pop up in modern c++. And memory leaks in terms of ever growing maps still happen. But the only times I have seen use after free or similar in modern code is where there was a bit that wasn't so modern. Is there some list for clang tidy modernize that will treat using anything other than value semantics as a warning?
- deleted 4y ago[deleted]
- nicolapede 4y agoWhat are the new language features that let you avoid using smart pointers?
- jokoon 4y agoI was called inexperienced because I was saying pointers are not 100% necessary in C++ anymore. I'm not even certain if using stl containers always use the heap. I'm a bit confused at times, since I avoid using oop and inheritance, I prefer data oriented, I just write functions and avoid side effects as often as possible.