I’m now writing a USB driver in Zig (and I’m feeling Zig is a great language…!).
In this area, it’s common that we have to place a struct in the specified pre-allocated heap (or somewhere) region:
I wonder that is there any convenient way to initialize a struct when we have a pointer to the memory to put the struct. It’s like new keywords does in C++.
I want it to:
I think your @ptrCast+@alignCast is most of it, you might want to follow that up with zeroInit and use its init argument to set some of the fields that you want to initialize to different values.
Note that zeroInit defaults values to zero, where the ptr.* = SomeStruct{} syntax would force you to provide the fa field initialization value explicitly, because it has no declared default.
Wow, this is another thing I was looking for…! Though I can’t use this function in this case cuz I want to use many non-default values for initialization, I think this function is really useful! Big thanks.