Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>pointer arithmetic in deep nested loops

What's wrong with that? Pointer arithmetic is just indexing with different syntax.



Nothing if you do it right. Indexing also goes wrong if you do it wrong. In both cases, they make sense as abstractions, but are less safe than certain other abstractions

I see lots of good code in the linux kernel that uses explicit indices and pointer arithmetic. It's written by people who know what they're doing with it, and perhaps more importantly, have hundreds of maintainers gating updates directly with review and thousands of open-source enthusiasts who might notice a problem

However, many modern languages, including C++, can do a lot of the things you might do in this explicit way in other ways that are considerably harder to screw up, and often drastically more efficient to boot, which was the case here. I think the guy I was interacting with wasn't even the one who wrote the code, he just had the vague sense that really good coders could write code this way, so it must be a good way to go about writing code. My point isn't that it's inherently bad or going to fail necessarily, just that it's the kind of pattern people mean when they complain about older languages without as many guardrails. This particular code was causing a memory leak that eventually crashed the device in some but not all of the contexts that called it, not because it was pointer arithmetic, but it had stayed broken and gotten more broken because the ways in which it was done wrong were harder to notice and suss out in that form than what I eventually worked it into, and the way that I did that used newer idioms (mostly involving lambda capture, moving some of the implicit polymorphism accomplished by casting pointers into explicit templated functions, and moving things into vectors to isolate where things were going off the ends of arrays, nothing super fancy, but newer than this guy who hadn't worked in C++ in a decade had seen), and we were all stressed out but it was definitely annoying that I was getting all these notes on interim commits that were basically a guy who was not on that project and did not understand the code trying to flex seniority on me by insisting that doing stuff in a way he didn't understand was wrong and bad. To his credit, after some of the stress had passed he did try to learn some of the functional-first kit of idioms I had used to fix the issue (once he saw that this indeed was what had happened), but it still left a sour taste in my mouth. I really dislike most interactions that are deeply concerned with status

I've written a lot of C/C++ and I've seen the cases where it makes sense, sometimes even is better to use the kinds of patterns that motivated language design choices like explicit pointers and casting and calling new all the time to allocate class instances, where those things still are necessary or efficient or even just make sense. But also, they are the kinds of things people complain about when they say that the language lets you make a lot of mistakes many other languages, especially newer ones, don't. I often write in subsets of C++ that have this property, and converting code I'm not ultra familiar with to equivalent functionality using different idioms just to see is often a productive way of finding and eliminating difficult bugs. This doesn't mean no one can or is writing good nested loops with pointer arithmetic driving the iteration, but it's a place to look for errors because it's harder to be good at that than it is to write a range-based for loop




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: