#type '[*c]const u8' does not support field access

1 messages · Page 1 of 1 (latest)

keen plover
#

I am trying to copy a C array into a slice:

    const spv = try alloc.alloc(u8, c.shaderc_result_get_length(result)); // []u8
    const bytes = c.shaderc_result_get_bytes(result); // [*c]const u8
    @memcpy(spv, bytes);

But unfortunately I get this error:

 error: type '[*c]const u8' does not support field access

I read the docs of @memcpy. Source is a multi item pointer and dest is a slice, and they are both u8 types, so I don't get why this copy does not compile.

wicked summit
#

[*c] is not a many item pointer, it's a c pointer

fierce hollow
#

surely it should allow that since C pointers coerce?

keen plover
#

Why doesn't it automatically cast to [*]const u8? I have to manually cast it.

wicked summit
#

it probably should be allowed, but my guess is that it isn't checking that case

runic tapir
#

Isn't it a problem with nullability?

keen plover
#

Well, ?[*c] is a thing, but it's not the case here

fierce hollow
#

C pointers are nullable by default, they map as close as possible to pointers in C

fierce hollow
#

havent run any tests yet tho so idk if it causes any breakages

wicked summit
#

that is probably the right place, but as stnap mentioned, c pointers are nullable, so you can't just use them like that

fierce hollow
#

why not? arent C pointers generally used like anything else but assumed to not be null?

#

assumed in the same way as in C, youre kinda meant to check it before using it but theres no automatic safety for you

wicked summit
fierce hollow
#

well i get that

#

i mean that theyre kinda more like allowzero pointers, youre not required to unwrap them before use. i dont understand wym by they cant be used like this

wicked summit
#

memcpy requires non optional pointers, because it does not check for null

fierce hollow
#

thats kind of my point, C pointers are always used in the same context as non optional pointers with an assumption that its not null

wicked summit
#

then they're being used wrong

#

assuming a nullable pointer is not null is how we get c-style null pointer dereferences

#

the short simple answer is that you should be avoiding [*c] at all costs, they should be replaced or casted to the correct zig pointer type

earnest urchin
#

I'm guessing that @memcpy([]u8, [*]const u8) would use the dest's length as the copy length?