Why do you have to specify calldata or memory for complex types like (string, array, struct etc)?

1 Answer 1


This is because solidity is a stack based language. You can see similar behavior in C or C++ for example, those are also stack based and the alternative storage space for dynamic types is the heap.

The stack is handled by the instructions generated at compilation time, not directly by you. You might already see an issue in having the logic defined at compilation time to handle objects like variable length arrays that can change at execution time and messup the structure expected at compilation time.

To overcome this issue, dynamic types are handled by references (like pointers in C or C++), a reference is simply an address (not like an ETH address, but an address in the actual memory). This address is a constant size element pointing to a variable size object (the actual data) in a specific location.

This reference could point to an address either in memory or calldata (assuming that we are only dealing with function parameters here). You must specify in which it is located, as the compiler simply cannot infer it for you.

  • thanks for your answer, I understand that it cannot be stored directly on the stack itself but what is the difference in storing ref types in calldata vs memory besides the fact that with calldata assignments are immutable? What about value types like uint or address do they get stored on the stack like they would be in C++? Nov 23, 2021 at 20:36
  • Check this answer : ethereum.stackexchange.com/a/74443/84305. OPCODES almost always consume their parameters from the stack, so value types will eventually be in the stack. But they might be loaded from memory / storage or calldata. From my understanding (which is certainly far from perfect) only short lived data (like counters) and constants are stack only due to the hard limit of 1024 call stack size. It might also depend on your optimizer settings.
    – hroussille
    Nov 23, 2021 at 21:28
  • okay i see, but value types that are passed as parameters, since you do not have to specify weather they should be held in memory or calldata, what do they default to? Nov 23, 2021 at 21:43

Your Answer

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

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