4 ms·
When we were undergrads, I remember looking over Tom's shoulder at some Unix code he was studying, and I worked up my courage to ask what "*cp++" meant. He sai
by drfuchs 4y ago
When we were undergrads, I remember looking over Tom's shoulder at some Unix code he was studying, and I worked up my courage to ask what "*cp++" meant. He said something about about "pointers" and "post-incrementing", but I couldn't figure out what he was talking about; he was a true wizard. Hi, Tom!
- teaearlgrey_hot 4y agoA wizard never uses the base pointer or the stack pointer by compilation: he moves the stack precisely as he means to!
- pugs78 4y agoThanks, Dave. You're no slouch yourself.
- trevcanhuman 4y agoUsing the post increment operator for pointer operations ? Would have never imagined that. I've only seen it used in for loops for no reason at all, really.
- pubby 4y agoIt's common enough, like in a typical memcpy implementation: while(size--) *dst++ = *src++;
- deleted 4y ago[deleted]
- MereInterest 4y agoOr chaining it even further for copying a null-terminated string. The assignment operator returns the same value that was just stored, so it can be used as the conditional on a while loop. while(*dst++ = *src++); Incredibly succinct, ridiculous powerful, and way too easy to overrun array bounds.
- Annatar 4y agoYep, if there is no #$00 ('\0') at the end of the string, an exploit will ensue, especially if the attacker manages to modify the string first.
- Taniwha 4y agoIt's one place C shows its roots .... mov (r0)+, (r1)+ is a PDP11 instruction as is mov -(PC), -(PC)
- noclues 4y ago
- Annatar 4y agoIt's very common, like ABC of pointer arithmetic. Especially if one has coded in assembler first, then took up C, it's the most natural thing to do.
- jjav 4y agoI miss Sun so much. Nearly all the happiest geek-out years of my career were spent there. A great technology company all about the technology. Today the so-called tech giants are not really tech companies. Advertising, online shopping, more advertising, toxic social networks, movies. None of them are selling technology as the actual product. Apple is the sole exception, but they are a locked-down walled-garden consumer products. Contrast with Sun which was all about interoperable open systems. I wish a company as remotely as cool as Sun still existed. I'd drop everything and go there right now.
- salgernon 4y agoI was #12386 at sun - circa 1990 but I don’t remember the exact date. The day I showed up I was given a cardboard box of VME boards with a sun 4/400 chassis, an Ethernet drop and and a SunOS tape and and left to myself to make it work. I think that might have been their way of hazing the new guy - and I’m sure I got nowhere without help. But it was a lot of fun. While I’m there in memory lane and apropos of the pointer post increment discussion - one of my first source checking there was when I “optimized” a bit of graphics code by rewriting it to use pointer arithmetic rather than array indices like the “idiot” that wrote it had. Then the “idiot” in question, who happened to have designed the chip the code was taking to, explained how his code took exactly so many cycles with such and such cache consideration and then I learned how to revert a change in SCCS.
- jjav 4y ago> But it was a lot of fun. Indeed! On my first day I was given a non-working SPARC box and told to get the latest Solaris working (in the early days of Solaris, when many were on SunOS). So much fun! Want to receive email? Make sure to configure sendmail correctly. And NFS to get to teamware. And so on. Never been so happy in a tech wonderland. Difficult to explain to a generation raised on third-party paid cloud services. > Then the “idiot” in question, who happened to have designed the chip the code was taking to The deep hardcore tech expertise across the board was so incredible at Sun. Nowhere before or after have I experienced that. Miss it a lot.