3 ms·
It's still weird to me that you can declare an array with the register keyword. Then it (understandably) becomes UB to attempt to get the pointer. (It also pr
by RobotToaster 4mo ago
It's still weird to me that you can declare an array with the register keyword.
Then it (understandably) becomes UB to attempt to get the pointer.
(It also probably isn't stored in a register, since the keyword is just asking the compiler nicely.)
- flohofwoe 4mo agoThe meaning of the 'register' keyword has changed over time to just "it's illegal to take the address of this item": https://www.godbolt.org/z/TKq9rWzP1 https://www.godbolt.org/z/TKq9rWzP1 Don't know what's the idea behind not allowing to take the address of a value though.
- randomNumber7 4mo agoIt was always only a suggestion to the compiler, to hold this variable in a register. Compilers got so good at optimization that there is little point using it. If a variable is held in a register you can't access it with a pointer. So if your intention is it should be in a register you can't take the address.
- tardedmeme 4mo agoIt once told the compiler to hold the value in a register because the compiler wasn't very smart at all.
- RobotToaster 4mo agoA register isn't in external memory, so isn't addressable as such. That part makes sense since if the compiler actually follows your suggestion it can't be addressed. Thinking about it, storing arrays in registers would possibly make sense on systems like the 8051 where you actually have a bunch of general purpose register banks, but those don't exist in x86.
- wasmperson 4mo agoIt matters in single-pass compilers. You can't allocate a variable in a register if its address is ever taken, but by the time a single-pass compiler knows that information it has already spit out all of the assembly for the function.