Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of bounds array access #8364

Closed
duytai opened this issue Feb 21, 2020 · 3 comments
Closed

Out of bounds array access #8364

duytai opened this issue Feb 21, 2020 · 3 comments

Comments

@duytai
Copy link

duytai commented Feb 21, 2020

Description

pragma solidity ^0.6.1;
contract A {
  function main() public {
    uint[3][4] memory balances;
    msg.sender.send(balances[2][3]);
  }
}

Compiler returns error: Out of bounds array access at msg.sender.send(balances[2][3]);

Environment

  • Compiler version: 0.6.1
  • Target EVM version (as per compiler settings):
  • Framework/IDE (e.g. Truffle or Remix):
  • EVM execution environment / backend / blockchain client:
  • Operating system: Mac 10.15.1

Steps to Reproduce

Compile with the above source code

@chriseth
Copy link
Contributor

Solidity does not have multi-dimensional arrays, it only has arrays of arrays. Because of that, uint[3] is an array of size 3, while uint[3][4] is an array of size 4 containing arrays of size 3.

@rschumi0
Copy link

rschumi0 commented Mar 5, 2020

However, even for nested arrays, why do you start with the size of the inside array? When you access it you have to start with the index of the outside array and then continue inwards.

For example, when I had an array with a bit more nesting like this:
a = [[[[3,1],[5,9]],[[6,8],[4,0]],[[1,9],[6,4]]],[[[0,4],[8,8]],[[10,6],[8,4]],[[2,10],[1,5]]],[[[8,3],[3,4]],[[0,4],[6,1]],[[10,8],[7,1]]]];
I was quite confused when the definition was like this uint8[2][2][3][3] and the access to the elements was in the reverse order (e.g., to the last element a[2][2][1][1]).

So why aren't the definition and the access consistent, i.e., by starting both from the outside?

@chriseth
Copy link
Contributor

chriseth commented Mar 8, 2020

@rschumi0 I hope it is consistent: T[3] is always an array of size 3 with inner type T, regardless of what T is (it can be another array, a struct or a value type). If you have a variable x of type T[3], then x[2] is always of type T, regardless of what T is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants