#Debug printing a struct

1 messages · Page 1 of 1 (latest)

devout magnet
#

How to print every field in a struct for debuging

#

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))}); }

frosty condor
#

zigs formatting does that by default so you can just pass it to a print function

#

std.debug.print("{}", .{your_struct});

devout magnet
#

this only prints what I assume is the internal mangled name of either the struct class or the struct instance

frosty condor
#

are you passing the struct type or an instance

#

it should print the fields

devout magnet
#

instance

frosty condor
#

show code

devout magnet
#

does this works even if the struct was declared in a .h?

frosty condor
#

it should

devout magnet
#

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

gusty ore
#

thats an opaque handle type, the fields aren't even specified

devout magnet
#

that makes lots of sense

gusty ore
#

its just a typedef struct VkPhysicalDevice_T* VkPhysicalDevice;

devout magnet
#

on that note, is = undefined the best way to 0 initialize a struct, seems kind of wrong to me lol

gusty ore
#

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

frosty condor
#

you wont be able to zero initialize an opaque type on the zig side

devout magnet
#

?

frosty condor
#

zig has no knowledge of the fields so no

gusty ore
gusty ore
frosty condor
#

the pointer being null wouldnt make the struct null tho

devout magnet
#

but doesn't zig asume all c pointers are nullable?

frosty condor
#

i assume youre meant to pass it to some vulkan function for initialization

gusty ore
devout magnet
gusty ore
#

but what exactly is the problem here? theres no reason to zero-init a VkPhysicalDevice, thats only ever an opaque handle to the device

devout magnet
#

it makes it convenient since some functions have docens of posible arguments

devout magnet
#

ok, I will then change my question lol,

#

how am i supposed to zero initialize a struct?

gusty ore
frosty condor
#

translate-c structs are zero initialized by default now

gusty ore
#

oh really thats neat

frosty condor
#

so for non opaque types you can just do var foo: c.Bar = .{};

devout magnet
#

thats great

gusty ore
#

are you using translate-c to generate your own vulkan bindings?

gusty ore
devout magnet
#

okay, I will use it from now on

devout magnet
gusty ore
#

what do you mean?

devout magnet
#

when I use translate-c it doesn't generate the code with the 0 defaults

gusty ore
#

then you probably don't have a recent enough compiler

devout magnet
#

isn't that feature on 0.11.0?

frosty condor