6

Let's say I have a dynamic array of some length and I want to push a new element, but memory after last elem is already used. enter image description here

What would happen in such case? Compiler would find a new 'chunk' of memory that can hold an array and copy it there or something else? And if yes, how this new address would be calculated?

Also, as I know, dynamic arrays in solidity works like mappings in some way, I mean each item of the array is laid out sequentially from address that is calculated with hash of array variable address:

keccak256(bytes32(variable_address))

And if memory was reallocated, how would solidity find this variable's new location? We don't store any pointers, just calculating hash.

enter image description here

1 Answer 1

6

Solidity never reallocates storage arrays (memory arrays can't even be resized). Solidity relies on the fact that all storage array locations are based on a KECCAK256 hash, and that KECCAK256 is believed to be collision-resistant, so the odds of two storage arrays ever colliding are infinitesimally small.

Your Answer

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

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