I've came accross this post that explains that in C, structs can't be zero-sized and must have at least one named field to not be UB.
However, in Rust, we can have such structs. And we can also mark structs as #[repr(C)]
that will, from the docs, "do what C does".
So naturally, I went out to the playground to try this:
#[repr(C)]
struct ZeroSized;
fn main() {
println!("Size: {}", std::mem::size_of::<ZeroSized>());
}
And this code does print out 0.
I was wondering does Rust allows #[repr(C)]
zero sized structs if they are forbidden, and even UB, in C?
Will the final size of such a struct actually be 0? What would happen if I copy this struct around in memory? Can I even have a pointer to it?