Skip to content

Rust C Structure alignment mismatch? #19189

Closed
@donpdonp

Description

@donpdonp

I'm trying to craft a binding from rust into mruby (sebastianedwards/rust-mruby is obsolete). In debugging a segfault in a foreign function, I get conflicting answers as to the alignment of a rust struct.

#[repr(C)]
struct MRubyValue {
  value: [u8, ..8],
  tt: MRubyType,
}

Printing the size and align gives

    println!("MRubyValue size {} align {}", std::mem::size_of::<MRubyValue>(),
                                            std::mem::align_of::<MRubyValue>());
=>
MRubyValue size 12 align 8

I found some code on the internet which supposedly prints the same information, but the alignment is different.

    let tyd =
        unsafe {
            (*std::intrinsics::get_tydesc::<T>())
        };
    println!("name: {} size: {} align: {}", tyd.name, tyd.size, tyd.align);
=>
name: MRubyValue size: 12 align: 4

Is this a rust problem or a misunderstanding of the output? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions