C program reads gibberish when given a Rust-created struct

⚓ Rust    📅 2026-01-14    👤 surdeus    👁️ 2      

surdeus

Hello, I am working on a Rust program that loads in C library files as plugins, I am currently facing this very weird behaviour about Rust structs.

I have defined 2 structs in Rust with repr C

#[repr(C)]
struct TwoStruct {
    pub one: u64,
    pub two: u64,
}

#[repr(C)]
struct ThreeStruct {
    pub one: u64,
    pub two: u64,
    pub three: u64,
}

And the same struct is defined in C

struct TwoStruct {
  uint64_t one;
  uint64_t two;
};

struct ThreeStruct {
  uint64_t one;
  uint64_t two;
  uint64_t three;
};

When i pass TwoStruct { one: 1, two: 2 } to a C function that prints out the struct contents, it prints out 1, 2 as expected. But when I pass ThreeStruct { one: 1, two: 2, three: 3 } it prints out gibberish instead of the expected 1, 2, 3.

Why?

I have included a repo so you can run the code, more info in README

4 posts - 2 participants

Read full topic

🏷️ Rust_feed