-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
offset_of! overflows stack when called on BIG structs #22
Comments
I'm guessing I think the answer here is going to be: don't use |
Looks like going into release mode fixes the stack overflow. So in order to test with debug assertions, I'd have to tweak an existing profile. I guess it works.
Isn't that a pretty big use-case for |
I'm not sure what you mean by this? References can be used to access large struct fields, the problem is creating the struct on the heap without first blowing up the stack. |
You cannot create a reference to uninitialized memory, that is undefined behavior. So you have to initialize the struct on the heap without creating a reference. In order to do this, to initialize memory, you have to write things into it. In order to write things into it, you need to know where the fields are that you're writing... |
I see. Depending on the nature of your struct, there may be other ways to initialize it. For example, if it consists entirely of fields for which zero is a valid bit pattern, then you can initialize the memory with zeros, and then create valid references to fields within it. The stack overflow is a problem with the underlying Also, there's actually no practical way to make this work without causing UB. Determining the offset requires a non-dangling pointer to the struct, and if we can't create it on the stack, then the only other option would be to allocate it on the heap, which means it wouldn't work without |
That's actually what I needed! This works: let allocation = NonNull::new(alloc(Layout::new::<Self>()) as *mut Self)
.expect("failed to allocate space");
write_bytes(allocation.as_ptr(), 0, 1);
Box::from_raw(allocation.as_ptr())
Closing because it's an upstream issue then. Thanks! |
I'm trying to initialize a struct on the heap, but
field-offset
just overflows the stack whenever I try to get the field offset xDThe text was updated successfully, but these errors were encountered: