News for hackers. Stuff that matters.
Re:You Can't Always Hash Pointers in C by arethuza on Tuesday May 31, 2016 @05:27AM (#11805579)
It's been a long time since I wrote C for a living, but what would you expect sensible behaviour to be for the expression ptr - array_start if ptr isn't in the array?
Re:You Can't Always Hash Pointers in C by userbinator on Tuesday May 31, 2016 @06:53AM (#11805804)
Do the subtraction and divide by the size of the pointed-to type, like the expression says? Why would you expect the calculation to be any different if ptr was inside, one element past, two elements past, or 1000 elements away from the ends of the array?

I can understand it possibly being a problem if the two pointers being subtracted denote completely different address spaces (which is definitely possible on some architectures, particularly Harvard MCUs; the one that comes to mind immediately is the 8051 which has IRAM, XRAM, and PMEM), and even there you can "linearise" address spaces so one comes after another and subtraction still works despite possibly giving a meaningless result, but on any architecture where all pointers are in the same address space, the sensible behaviour is the most consistent and straightforward one.

Re:You Can't Always Hash Pointers in C by caf on Tuesday May 31, 2016 @07:15AM (#11805866)
The idea here is that if you have segment:offset pointers, like x86 real mode, then you (as a compiler writer) don't have to worry about the segment part at all - you can just subtract the offsets and shift, because pointers within the same object will have the same segment part.
Re:You Can't Always Hash Pointers in C by qb45 on Tuesday May 31, 2016 @07:51AM (#11805985)
Just nitpicking, but in real mode it is at least conceivable to implement arbitrary pointer arithmetic (at some significant performance hit) because segments are spaced evenly every 16 bytes.

The real killer is arbitrary segmentation, where software can say this segment start here and that segment starts there, like, say, the x86 protected mode. Is the OS going to expose absolute segment addresses to the C language to allow some "defensive programming" like checking if a pointer really points to the array you think it does? Hell no, you write your damn code correctly ;)

Re:You Can't Always Hash Pointers in C by tomsmeding on Tuesday May 31, 2016 @07:13AM (#11805861)
You expect a pointer to represent a location in linear memory, so you expect ptr - array_start to be a negative number if ptr is before the array, and a number larger than the array size if it's after the array.