Pointer to a custom struct?
Is it possible to create pointers to a custom struct instance, and be able to load the instance from the pointer?
According to the AI bot, "Mojo's Pointer struct is designed to work with basic types like Int, and it's not clear if it can directly handle custom structs".
Here is my latest attempt (a linked list):
Which got an error:
3 Replies
The problem here seem to be the use of
Pointer[ListNode]
.
The code will work if it's Pointer[Int]
Any idea how to make the linked list work? ThanksThis compiles, but im not sure if it works
i believe things marked with @register_passable
aren't allowed to have dynamic memory and allocate to heap
im not sure, here from the docs:
"
1. @register_passable types are not able to hold instances of types that are not themselves @register_passable.
2. Instances of @register_passable types do not have predictable identity, and so the self pointer is not stable/predictable (e.g. in hash tables).
3. @register_passable arguments and result are exposed to C and C++ directly, instead of being passed by-pointer.
4. The init and copyinit methods of this type are implicitly static (like new in Python) and returns its result by-value instead of taking inout self.
"
Try using
I can’t test this right now but will probably try this later.