Obtain pointer from static array literal

codic codic at linuxmail.org
Fri Oct 8 02:49:17 UTC 2021


I am working with a C API (XCB) which uses `void*` a lot to 
obtain parameter packs; these are very small and throwaway so I 
want them to be allocated to the stack.

Of course, this is valid, but a bit painful:
```d
uint[4] params=[x,y,width,height];
func(params.ptr);
```
especially when you have lots of calls. now, this compiles:
```d
// (import std.array)
func([x,y,width,height].staticArray.ptr);
```
but is it well-formed? or is it UB because it is a rvalue and 
scope ends?
i.e. here is an example program, is it well formed?
```d
import core.stdc.stdio, std.array;

void thing(int* abc) {
   printf("%d\n", *abc);
}

extern(C) void main() {
   thing([1].staticArray.ptr);
}
```
At any rate, it runs fine consistently with ldc:
```
$ ldc2 test.d -fsanitize=address
$ ./test
1
```

I don't know if it is spec-compliant though, or if it is actually 
causing UB here and I don't notice it.


More information about the Digitalmars-d-learn mailing list