1

I have this field on the data account of my pda:

#[max_len(0)]
    pub users: Vec<Pubkey>,

But I'm still able to push a value to it, even though it should have 0 space allocated. Why is that so? Also, when I have the init attribute for the #[accounts()] attribute macro, does the fields of the account being initialized are initialized using Default e.g. Option is initialized to None and Vec to empty?

1
  • My understanding was that it works with the InitSpace macro to figure out what is the required number of bytes to be allocated on init (i.e., to be possible to use INIT_SPACE with types like String) and that there is no other functionality of the declaration (anchor-lang.com/docs/space#the-init-space-macro).
    – chalda
    Commented Apr 18 at 7:45

1 Answer 1

3

You shouldn't be able to push elements if you set max_len to 0. You might have space elsewhere as a buffer, or you might have allocated more space when you init the struct.

Option and Vec would both init to all zeroes. Vec is some overhead (about 4 bytes) and a then zeroes for whatever space your data would take. Option is 1 byte plus whatever the data is, where 0 is None. See https://book.anchor-lang.com/anchor_references/space.html

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.