0

suppose I have an array with 2 ^ 256-1 elements What happens if I add another element?

  • 1
    There is no known scenario in which this premise is economically feasible. – Rob Hitchens May 30 '18 at 15:24
  • but ipotetically is not possible? – Alfredo Egaf May 30 '18 at 15:29
  • You are asking for an array of 1.1579209e+77 elements, let's say these are uint256, that is 1.0531229e+65 Tera bits !!!!!!!!!! – Jaime May 30 '18 at 16:11
0

Contract to test

pragma solidity ^0.4.0;

contract Bar {
    uint256[] public data;
    constructor() public {
        data.length = uint256(~0);
    }
    function foo(uint256 value) public {
        data.push(value);
    }
}

At the beggining in remix you can access any index between 0 and 0xFFFF..FF, the returned value is always 0.

If you call foo(1234) it will succeed but the array will appear to be empty.

If you call foo(1234) a second time it will succeed and the array will have a single value 1234 at index 0.

| improve this answer | |
0

I already have my answer: in fact what overflow does is the length, then the array is inaccessible, although it is not erased but it is still adding data at the end starting from scratch

| improve this answer | |
  • This is because the way an array is stored is as [length, data] where the word at the address of the array stores the length, and then the data sequentiality after it. If you wrap around then you end up writing to the space storing the length. – Tjaden Hess Jun 1 '18 at 5:00

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.