I've used the code from the solidity assembly tutorial here to create a single dimensional array in assembly. The code is as follows:
contract C {
function f(uint a, uint b) constant returns (uint[]) {
assembly {
// Create an dynamic sized array manually.
let memOffset := mload(0x40) // 0x40 is the address where next free memory slot is stored in Solidity.
mstore(memOffset, 0x20) // single dimensional array, data offset is 0x20
mstore(add(memOffset, 32), 2) // Set size to 2
mstore(add(memOffset, 64), a) // array[0] = a
mstore(add(memOffset, 96), b) // array[1] = b
return(memOffset, 128)
}
}
}
When I run the function by itself in Remix, everything is ok, and it outputs the array in the correct format. The issue occurs when I try to index any value in the array when it's returned from another function. It seems to return an array of size 32, with all elements set to 32.
function get_f(uint a, uint b) public returns(uint){
uint[] ret = f(a,b);
return(ret[0]); //should return a, instead returns 32.
}
The above demonstrates the problem more clearly, but I am in fact using this method of array creation to cheaply convert between uint and byte arrays directly in memory (for use with the new precompiled modexp contract). thanks.