why is the behavior of pointers to 0 width doing this?
monkyyy
crazymonkyyy at gmail.com
Fri Jul 4 17:39:38 UTC 2025
On Friday, 4 July 2025 at 08:17:34 UTC, Richard (Rikki) Andrew
Cattermole wrote:
> ```d
> import std.stdio;
>
> __gshared int literal;
>
> void main() {
> int[0] callstack;
> (&callstack+literal).writeln;
> }
> ```
>
> ```asm
> _Dmain:
> .Lfunc_begin0:
> .file 1 "/" "app/example.d"
> .loc 1 5 0
> .cfi_startproc
> pushq %rbp
> .cfi_def_cfa_offset 16
> .cfi_offset %rbp, -16
> movq %rsp, %rbp
> .cfi_def_cfa_register %rbp
> subq $16, %rsp
> movq _D7example7literali at GOTPCREL(%rip), %rax
> movslq (%rax), %rax
> leaq -4(%rbp), %rdi
> imulq $0, %rax, %rax
> addq %rax, %rdi
> .Ltmp0:
> .loc 1 7 5 prologue_end
> callq _D3std5stdio__T7writelnTPG0iZQoFNfQkZv at PLT
> .loc 1 8 1
> xorl %eax, %eax
> .loc 1 8 1 epilogue_begin is_stmt 0
> addq $16, %rsp
> popq %rbp
> .cfi_def_cfa %rsp, 8
> retq
> ```
>
> Its the famed equation ``ax+b``.
>
> But with ``a * x`` becoming ``0`` by the frontend.
```d
import std;
unittest{
void* callstack;
(callstack).writeln;
(callstack+1000).writeln;
}
```
Im confused why 0 stride pointers behave differently then void*;
shouldnt they be vaguely similar?
More information about the Digitalmars-d-learn
mailing list