0

zero_copy and repr(c) are used before state struct to handle the over-stack problem. While generating the idl json file by anchor build, no padding field is generated following #[repr(C)] alignment rule. How to generate these padding fields automatically? Is there a parameter I can use while running 'anchor build' for this. I need this to fetch and deserialize the state account correctly.

#[account(zero_copy(unsafe))]
#[repr(C)]
pub struct SSS {
pub u8 f1,
pub u64 f2,
pub u8 f3
}

1 Answer 1

0

The padding will always get created automatically without your knowledge. If you create this struct as repr(C) and get its size, you'll have:

println!("{}", std::mem::size_of::<SSS>());

give you an answer of 24, which means 7 bytes of padding after f1 and 7 bytes after f3. You can declare it explicitly if needed:

#[repr(C)]
pub struct SSS {
    f1: u8,
    padding_after_f1: [u8; 7],
    f2: u64,
    f3: u8,
    padding_after_f3: [u8; 7],
}

Which will also give you a size of 24.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.