2

If a structure contains a nested structure and it is deleted, does the nested structure get deleted as well?

For example,

struct Point {
    uint256 x;
    uint256 y;
}

struct Image {
    Point[] points;
    string name;
}

If image is deleted does the points array get deleted as well?

1 Answer 1

3

Yes everything gets cleared

Given this contract:

contract C {
    struct Point {
        uint256 x;
        uint256 y;
    }

    struct Image {
        Point[] points;
        string name;
    }
    Image img;
    function C(){
        img.points[0]=Point({x:1,y:1});
        img.name= 'sebi';
    }

    function func() {

        delete img;
    }
}

The relevant EVM assembler looks like this, with a lot of PUSH 0's followed by 5 SSTORE which is an indication that it is cleared all:

    JUMPDEST            function func() {\n        
  ...
      PUSH 0            img
      PUSH 0            img
      PUSH 0            delete img
      DUP3          delete img
      ADD           delete img
      PUSH 0            delete img
      POP           delete img
      DUP1          delete img
      SLOAD             delete img
      PUSH 0            delete img
      DUP3          delete img
      SSTORE            delete img
      PUSH 2            delete img
      MUL           delete img
      SWAP1             delete img
      PUSH 0            delete img
      MSTORE            delete img
      PUSH 20           delete img
      PUSH 0            delete img
      SHA3          delete img
      SWAP1             delete img
      DUP2          delete img
      ADD           delete img
      SWAP1             delete img
      PUSH [tag] 8          delete img
      SWAP2             delete img
      SWAP1             delete img
      PUSH [tag] 9          delete img
      JUMP          delete img
    tag 9           delete img
      JUMPDEST          delete img
      DUP1          delete img
      DUP3          delete img
      GT            delete img
      ISZERO            delete img
      PUSH [tag] 10         delete img
      JUMPI             delete img
      PUSH 0            delete img
      PUSH 0            delete img
      DUP3          delete img
      ADD           delete img
      PUSH 0            delete img
      POP           delete img
      PUSH 0            delete img
      SWAP1             delete img
      SSTORE            delete img
      PUSH 1            delete img
      DUP3          delete img
      ADD           delete img
      PUSH 0            delete img
      POP           delete img
      PUSH 0            delete img
      SWAP1             delete img
      SSTORE            delete img
      POP           delete img
      PUSH 2            delete img
      ADD           delete img
      PUSH [tag] 9          delete img
      JUMP          delete img
    tag 10          delete img
      JUMPDEST          delete img
      POP           delete img
      SWAP1             delete img
      JUMP          delete img
    tag 8           delete img
      JUMPDEST          delete img
    tag 7           delete img
      JUMPDEST          delete img
      POP           delete img
      PUSH 1            delete img
      DUP3          delete img
      ADD           delete img
      PUSH 0            delete img
      POP           delete img
      DUP1          delete img
      SLOAD             delete img
      PUSH 1            delete img
      DUP2          delete img
      PUSH 1            delete img
      AND           delete img
      ISZERO            delete img
      PUSH 100          delete img
      MUL           delete img
      SUB           delete img
      AND           delete img
      PUSH 2            delete img
      SWAP1             delete img
      DIV           delete img
      PUSH 0            delete img
      DUP3          delete img
      SSTORE            delete img
      DUP1          delete img
      PUSH 1F           delete img
      LT            delete img
      PUSH [tag] 12         delete img
      JUMPI             delete img
      POP           delete img
      PUSH [tag] 11         delete img
      JUMP          delete img
    tag 12          delete img
      JUMPDEST          delete img
      PUSH 1F           delete img
      ADD           delete img
      PUSH 20           delete img
      SWAP1             delete img
      DIV           delete img
      SWAP1             delete img
      PUSH 0            delete img
      MSTORE            delete img
      PUSH 20           delete img
      PUSH 0            delete img
      SHA3          delete img
      SWAP1             delete img
      DUP2          delete img
      ADD           delete img
      SWAP1             delete img
      PUSH [tag] 13         delete img
      SWAP2             delete img
      SWAP1             delete img
      PUSH [tag] 14         delete img
      JUMP          delete img
    tag 14          delete img
      JUMPDEST          delete img
      DUP1          delete img
      DUP3          delete img
      GT            delete img
      ISZERO            delete img
      PUSH [tag] 15         delete img
      JUMPI             delete img
      PUSH 0            delete img
      DUP2          delete img
      DUP2          delete img
      POP           delete img
      PUSH 0            delete img
      SWAP1             delete img
      SSTORE            delete img
      POP           delete img
      PUSH 1            delete img
      ADD           delete img
      PUSH [tag] 14         delete img
      JUMP          delete img
    tag 15          delete img
      JUMPDEST          delete img
      POP           delete img
      SWAP1             delete img
      JUMP          delete img
    tag 13          delete img
      JUMPDEST          delete img
    tag 11          delete img
      JUMPDEST          delete img
      POP           delete img
      POP           delete img
      POP           delete img
    tag 6           function func() {\n     
1
  • I want to double check and see if I understand the assembly fully Commented Jul 19, 2016 at 9:57

Your Answer

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

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