Skip to main content
Just expanding on the explanation for why size_of is not useful for regularly serialized accounts
Source Link
Henry E
  • 2.9k
  • 7
  • 32

For zero_copy accounts, you can use mem::size_of(). But for non-zero_copy accounts, the alignment is off and size_of() will give you a larger, soincorrect space value. Instead you can do `

    let acc = Account::default();
    let size = x.try_to_vec().unwrap().len();`

Basically, you cannot use the mem::size_of for serialized accounts, you need to give the serialized size, which can be inferred from the Default trait of the account

For zero_copy accounts, you can use mem::size_of(). But for non-zero_copy accounts, the alignment is off, so you can do `

let acc = Account::default();
let size = x.try_to_vec().unwrap().len();`

Basically, you cannot use the mem::size_of for serialized accounts, you need to give the serialized size, which can be inferred from the Default trait of the account

For zero_copy accounts, you can use mem::size_of(). But for non-zero_copy accounts, the alignment is off and size_of() will give you a larger, incorrect space value. Instead you can do

    let acc = Account::default();
    let size = x.try_to_vec().unwrap().len();`

Basically, you cannot use the mem::size_of for serialized accounts, you need to give the serialized size, which can be inferred from the Default trait of the account

Source Link
hrs8
  • 146
  • 1

For zero_copy accounts, you can use mem::size_of(). But for non-zero_copy accounts, the alignment is off, so you can do `

let acc = Account::default();
let size = x.try_to_vec().unwrap().len();`

Basically, you cannot use the mem::size_of for serialized accounts, you need to give the serialized size, which can be inferred from the Default trait of the account