#Debug printing a struct
1 messages · Page 1 of 1 (latest)
found some code that said
inline for (std.meta.fields(@TypeOf(x))) |f| { std.log.debug(f.name ++ " {any}", .{@as(f.type, @field(x, f.name))}); }
zigs formatting does that by default so you can just pass it to a print function
std.debug.print("{}", .{your_struct});
this only prints what I assume is the internal mangled name of either the struct class or the struct instance
instance
show code
does this works even if the struct was declared in a .h?
it should
var application: c.VkPhysicalDevice = undefined; std.log.debug("full struct {?}", .{application});
and the output is
debug: full struct .<placeholder>.zig.zig-cache.o.11a0e6abe8f7d615c89001169bd5c984.cimport.struct_VkPhysicalDevice_T@aaaaaaaaaaaaaaaa
thats an opaque handle type, the fields aren't even specified
that makes lots of sense
its just a typedef struct VkPhysicalDevice_T* VkPhysicalDevice;
on that note, is = undefined the best way to 0 initialize a struct, seems kind of wrong to me lol
it does not zero-initialize, it leaves the memory uninitialized (= UB to access)
there is std.mem.zeroInit or std.mem.zeroes for that
but if possible the struct fields should just have a default value of zero and you can then initialize it with an empty literal, or by specifying only the fields you want to set
you wont be able to zero initialize an opaque type on the zig side
even with std.mem.zeroInit or std.mem.zeroes
?
zig has no knowledge of the fields so no
thats not necessary anyway, but its common to zero initialize the vulkan structs passed as arguments
you would create a null pointer, which is not allowed if the pointer type is not nullable
the pointer being null wouldnt make the struct null tho
but doesn't zig asume all c pointers are nullable?
i assume youre meant to pass it to some vulkan function for initialization
yes it does when using translate-c
structs are how vulkan handles you sending arguments to functions
but what exactly is the problem here? theres no reason to zero-init a VkPhysicalDevice, thats only ever an opaque handle to the device
it makes it convenient since some functions have docens of posible arguments
yes thats why I said this
I know, I only took a random struct from vulkan
ok, I will then change my question lol,
how am i supposed to zero initialize a struct?
best case this, or if not possible std.mem.zeroInit
when using https://github.com/Snektron/vulkan-zig all the relevant structs have default fields so the better approach works
translate-c structs are zero initialized by default now
oh really thats neat
so for non opaque types you can just do var foo: c.Bar = .{};
Okay
thats great
are you using translate-c to generate your own vulkan bindings?
because I'd strongly suggest using this, its great
okay, I will use it from now on
Do in need to update that individually?
what do you mean?
when I use translate-c it doesn't generate the code with the 0 defaults
then you probably don't have a recent enough compiler
isn't that feature on 0.11.0?