3 ms·
I actually have never wondered before: is it ever valid to use pointer 0?
by DiseasedBadger 7y ago
I actually have never wondered before: is it ever valid to use pointer 0?
- AndyKelley 7y agoYes, and here is an example: https://github.com/andrewrk/clashos/blob/91f45cff49128b3322f98cc78ed08df5bcec35cb/src/main.zig#L81 https://github.com/andrewrk/clashos/blob/91f45cff49128b3322f... When the raspberry pi 3 hardware boots, it jumps to 0x0. So this is the part where the bootloader loads the new kernel image into memory... starting at address 0!
- __turbobrew__ 7y ago> @intToPtr([]allowzero const u8, 0 [0..@ptrToInt(&__end_init)] vs > (int*)0 They sure make you pay though the nose to cast in zig -- maybe a good thing?
- AnIdiotOnTheNet 7y agoZig's type system tries to be aware of what its pointing to. Is it an array of comptime known length? An array of unknown length? An array of runtime known length? or a single item? Is it mutable? What's its alignment requirement? etc. It even has a pointer type for "something that came from C, who the hell knows". There is an accepted proposal to have pointer types for null-terminated blocks of memory too. So when you create a pointer out of thin air, you have to inform the type system precisely what kind of pointer it is. That's why it is so much more gnarly than C, it wants you to be explicit.
- jforberg 7y agoIn a system with virtual memory, you would typically ensure that address 0 (i.e. the lowest page) is unmapped. This causes an error condition if the address is ever read or written, which is useful because 0 is used as a placeholder value (i.e. NULL) in many situations. But this is a conscious decision by the system not to use this page, there's nothing technically stopping you from mapping it and putting some data at virtual address 0. In a bare-metal environment, there's nothing saying that physical address zero must be unused by the hardware designer. So I think it's quite the opposite: There's no requirement that address 0 must be unoccupied in any situation, it just so happens that many operating systems choose to leave it unmapped as a debugging aid for developers.