1

My function body looks like this:

function element(uint a, uint b, uint[2] memory c) public returns(uint e, uint f, uint g, uint k) {

I am wondering why do I put memory keyword before c ? I know that the argument parameters are stored in calldata. so by using calldataload, I successfully get a and b variables' values. The funny thing is by using calldataload , I also managed to get c(array) variable's first and second values.

So, turns out c is also in calldata location. Why do we specify memory then ?

  • 1
    No good reason AFAIK. The compiler assumes storage for every array and structure, unless memory is explicitly stated. Obviously, non of the function arguments can ever reside in storage, so this is clearly just a redundant limitation added by the authors of the compiler (probably made it easier on them to parse the code). – goodvibration Oct 2 at 16:10
  • haha, so funny. Thanks @goodvibration . So it means it's still on calldata – Nika Kurashvili Oct 2 at 16:11
  • NP, but keep in mind - AFAIK. Not all the memory-layout design and considerations are 100% clear to me either. I'm assuming that most of them follow the same pattern as C, with storage being (kind of) equivalent to data section in C (global and/or static variables), and non-storage being split into stack for variables of known length and heap for variables of unknown length. – goodvibration Oct 2 at 16:38
  • sounds good. I want to make sure about something since you mentioned known length. in a function, uint[2] test can't be created unless i specify memory or storage. So array has known length, but still it won't be on stack. It seems like by known length, you mean primitive types. right ? – Nika Kurashvili Oct 2 at 16:45
  • 1
    No, I actually mean known length. As I said, memory only tells the compiler that an array/struct is not in storage, as implicitly assumed by the compiler. I have also explained that at the end of my answer to one of your previous questions, which you seem to have overlooked (or ignored). However, at the beginning of that same answer, I did state that the distinguish (between stack and heap) is based on primitive/non-primitive type rather than on known/unknown length. I'm not sure which one it is, and to a certain extent, these two definitions overlap. – goodvibration Oct 2 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.