0

This compiles:

enter image description here

This don't:

enter image description here

Untitled:3:9: Error: Type uint8[2] memory is not implicitly convertible to expected type uint8[2] storage pointer.
        uint8[2] z = [255,255];
        ^--------------------^

What's going on behind the scenes in the second example?

2

From the Solidity documentation:

Is it possible to in-line initialize an array like so: string[] myarray = ["a", "b"];?

Yes. However it should be noted that this currently only works with statically sized memory arrays.

What is the memory keyword? What does it do?

The Ethereum Virtual Machine has three areas where it can store items.

The first is “storage”, where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use.

The second is “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.

The third one is the stack, which is used to hold small local variables. It is almost free to use, but can only hold a limited amount of values.

For almost all types, you cannot specify where they should be stored, because they are copied everytime they are used.

The types where the so-called storage location is important are structs and arrays. If you e.g. pass such variables in function calls, their data is not copied if it can stay in memory or stay in storage. This means that you can modify their content in the called function and these modifications will still be visible in the caller. There are defaults for the storage location depending on which type of variable it concerns:

  • state variables are always in storage
  • function arguments are always in memory
  • local variables always reference storage

So, in your code, the only thing that you need to do is:

contract A{
    function getZ() returns(uint8[2]){
        uint8[2] memory z = [255, 255];

        return z;
    }
}
  • I always forget to look through solidity docs FAQ. Thank you! One quick question: if I add to my function uint8 x = 255; it will be stored in the stack, the cheapest option? I also remember the depth of call stack is 1024. Does it mean that I can only make 1024 statements like uint8 x = 255;? – manidos Aug 4 '16 at 11:35
2

The problem is that z is a local array variable and in solidity local variables are by default located in storage. The array you pass is in memory and you cannot pass data from memory to storage for reference types like arrays. Also reference types local variables cannot be assigned data other than references to state variables since storage is allocated statically.

If you do uint8[2] memory z you will set the location of the local variable in memory and your code will be error free.

For some more details check the answer here: Type inaccessible dynamic type is not implicitly convertible

  • starting to get it. So, just to be clear, uint8[2] memory z = [255, 255]; doesn't cost any gas? – manidos Aug 4 '16 at 11:25
  • 1
    Im sure it costs gas because it is a computation executed by the evm – dragosb Aug 4 '16 at 11:27

Your Answer

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

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