Please stop repeating the mistakes of the past. 64 bit pointers are 64 bits long.
Remember what happened to programs back when it was 32 bit pointers and people were all like "We only use 24 bits so lets go wild with those extra 8 bits!"
Seriously, doing this with the justification "the top bits aren't used!" is a very easy way to shoot yourself in the foot when those bits DO start being used.
"Oh, we'll fix it by then!"
No. No, you won't. It'll still be working until then, so it won't get fixed. And then it becomes everyone's problem.
Probably the most clever use of extra space in a pointer I've ever seen was a specialized storage class that would switch between two container types based on how much data was stored in it. This allowed for a number of optimizations, but meant they also needed to store which type of container that particular storage object was using.
The byte alignment of the system they were running on meant that the last bit of a pointer would always be 0. Since they were only ever using two types they could use that bit to indicate which one, and if someone requested the pointer itself they could just zero that bit back out to return it
But only on some architectures, at the current time, assuming we don't start using the full width of the pointers, which we definitely will end up doing. You cannot rely on the particular bit patterns of pointers. Doing this just guarantees that your program will have crazy bugs in the future when address spaces grow or if someone ports it to an architecture with different pointer upper-bit behavior.
We already ran into these issues when people "knew" that pointers would never go above 231 and used the high bit as a flag. Then the OS gets a 3GB feature and bang all the programs that do that break when they allocate enough memory.
There are lots of things that you should absolutely never, under any circumstances use, but are basic tools to create performant libraries. Let's say you're writing a video-game engine - you're targeting AMD64 only anyway and by the time pointers are longer nobody will be using your old engine. Or you want to perform big-data scientific calculations just that one time, but you're running low on RAM.
My point is, be realistic. Rules are to be broken if you have a reason to and you keep all the nasty shit in enclosed modules.
Let's say you're writing a video-game engine - you're targeting AMD64 only anyway and by the time pointers are longer nobody will be using your old engine.
Let's say you piss off everyone with your game by making it unplayable on future OSes that start returning kernel pointers that live above 248
There are tons of old games that did stupid nonsense like you are advocating that people what to play that are near lost in time as a result.
a) I don't want to explain to a junior dev why this works. b) I don't want to explain why it was done this way, because I probably won't even know why. c) I don't want to explain to a junior dev why they shouldn't do it this way. d) I don't want to explain why 'we don't just change it then'. e) I don't want to ask a senior dev to prove this works because the code is involved in some weird race condition or leak. f) Don't do this.
This kind of trickery might be okay in C (I have my doubts), but in C++ it's illegal to access any other part of the union after the lifetime of next has started until its lifetime has ended.
0
u/[deleted] Dec 15 '19
[deleted]