Does the EVM always allocate new memory or can it reuse already allocated, but unused, memory?
Reading the docs for delete
(http://solidity.readthedocs.io/en/develop/types.html), it is mentioned that:
delete a assigns the initial value for the type to a. I.e. for integers it is equivalent to a = 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements reset. For structs, it assigns a struct with all members reset.
Does this mean that if a
is a dynamic array (uint[]
) of, say, length n
, then delete a
assigns to a
an empty array:a = new uint[](0)
? Not the original array, of length n
, with its contents set to 0
?
So, if new
and delete
are used many times in a Solidity contract, will the EVM keep allocating new memory (expanding memory), or will it reuse already allocated memory when possible?
Consider the loop:
uint[] a;
for (uint i = 0; i < 1000; ++i) {
a = new uint[](100);
delete a;
}
Will this allocate 100 or 100000 memory cells?