The calldata Storage

Learn how calldata works in Solidity.

calldata is recognized as the third type of data location in Solidity. It operates similarly to memory by serving as a temporary storage space. Although calldata is temporary storage, it differs slightly from memory. The primary distinction is that once function arguments are supplied, they can’t be changed inside the function because calldata is nonmodifiable storage. It’s less expensive than memory and is mostly utilized with external function types, which is the second significant distinction.

calldata is often utilized with external functions, and the this keyword allows us to invoke a function that’s declared with the external keyword inside the contract.

Key characteristics

The key characteristics of calldata are listed below:

  • Immutable nature: calldata is immutable, meaning that the executing function can’t modify or change the data stored in calldata. It’s a crucial property for ensuring the integrity of function arguments.

  • Similarity to memory: calldata mostly behaves like memory. However, unlike memory, calldata is read-only, providing an additional layer of security against unintentional modifications.

  • Avoiding unnecessary copies: Using calldata is recommended because it helps avoid unnecessary data copies. It allows functions to access and work with function arguments directly without duplicating the data.

  • Return of arrays and structs: Functions can return arrays and structs with the data location of calldata, further enhancing the flexibility of calldata in function interactions.

Format assumption and ABI specification

  • ABI specification compliance: calldata is assumed to be in a format defined by the ABI specification. This format includes padding the data to multiples of 32 bytes. This aligns with the ABI encoding rules and ensures consistent data handling.

  • Constructor arguments: Arguments for constructors are handled slightly differently. They’re directly appended to the end of the contract’s code in ABI encoding, providing a unique consideration for constructor argument handling.

Get hands-on with 1200+ tech skills courses.