3

I would like to declare a variable as a memory array of pointers to storage arrays of uint256.

For example:

pragma solidity ^0.4.24;

contract A
{
    uint256[] public array0;
    uint256[] public array1;
    constructor () public
    {
        array0.push(123);
        array1.push(456);
    }
    function test() public
    {
        uint256[][] memory storageArrays = new uint256[][](2);
        storageArrays[0] = array0;
        storageArrays[1] = array1;

        storageArrays[0][0]++;
        storageArrays[1][0]--;
    }
}

I want this to change the values of array0 and array1, but they don't change because the storage arrays were implicitly copied to memory. This code only changes their copy in memory.

This is because both dimensions of the array are set to memory. I would like the first dimension of the array to be storage, and the second one memory

I tried to declare storageArrays like this:

uint256[] storage [] memory storageArrays = new uint256[][](2);

But it gives this syntax error:

ParserError: Expected identifier but got '['
uint256[] storage [] memory storageArrays = new uint256[][](2);
                  ^

Is it possible? If so, how? Thanks in advance.

  • Excuse me if I'm wrong, but if even if you wanted to set the first dimension to storage and the data in that storage to memory, and you increment or decrement the data, it'd still only be a memory change for the data itself. – ReyHaynes Jul 17 '18 at 15:02
  • @ReyHaynes I want to put storage pointers in a memory array, not memory pointers in a storage array. – Jesbus Jul 17 '18 at 15:48
1

Solidity, in a deliberate way, does not support memory pointers “a la C”, neither it has any pointer arithmetic in place. This means that incrementing or decrementing a pointer variable is not a meaningful operation. You can do something similar in assembly, but you must manage the whole thing at the lowest level possible and without thinking to be able to sync the solidity variables and the assembly variables for any future version of Ethereum protocol.

| improve this answer | |
0

I get that you want to put storage pointers into a memory array (storageArrays[0] = array0) so you can update the storage value more efficiently later...

...unfortunately the reference to the storage pointer in those memory array storageArrays[0 or 1] is basically a memory copy, along with any updates to that copy itself, which is why you'll only see changes the incrementation and decrementation to the uint value in storageArrays change in memory.

As far as I'm concerned, it's not possible...and it would actually be a security risk having data that is temporary (memory) have the ability to point and change any data in hard storage.

At least in Solidity, this seems to be the case.

| improve this answer | |

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.