On Wednesday, 1 March 2023 at 08:26:07 UTC, FeepingCreature wrote:
11 is SIGSEGV. A segfault, or access violation, happens when
you try to access unallocated memory. In this case, let me
annotate your code so it's easier to see what's happening:
```d
// null is the default value for a pointer
uint* value = null;
// because `value` is null, the first index also lies at null.
assert(&value[0] is null);
// So we try to store screen.black_pixel at memory address
null, which is unallocated.
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value);
```
As there is no memory segment allocated at address null, the
CPU indicates a segmentation fault, which terminates the
program.
So yes, `xcb_create_gc` wants a `uint*` parameter, but not just
any `uint*` will do: it has to point to valid memory. Going
back to the first snippet, what's happening here is that in C,
arrays implicitly convert to pointers, because C doesn't have a
notion of array types as distinct from pointer types. So you
can have a variable declared as `uint[1]`, but pass it to a
parameter that expects `uint*`, and the value that is passed
will just be the address of the first field of the array.
However, even in C, if you try to define `value` as `uint*`, it
will segfault in the same way. Instead, in D, you need to tell
the compiler to define an array of size 1, and then pass a
pointer to the array's first member explicitly:
```d
uint32_t[1] value;
value[0] = screen.black_pixel;
// this is what C does under the hood
xcb_create_gc(connection, black, win, mask, &value[0]);
```
Or shorter, but with the same effect:
```d
uint32_t[1] value;
value[0] = screen.black_pixel;
xcb_create_gc(connection, black, win, mask, value.ptr);
```
Thank you! You are amazing for explaining it! I was so focused on
thinking that I'm doing something wrong with the type that I
didn't noticed that the pointers, points to nowhere so the
function obviously has nowhere to write to. Like... OMG! And I
want to make a fully fledged compiler when making stupid mistakes
like that. Btw, When I executed the program, I got "Error Program
exited with code -11". You said that the code was "11". What
about that dash? If it is not a "minus" and it's just the dash
symbol, then what's the idea?