There are two things that are bugging me about the below smart contract:
- In the below contract, I noticed that the orders variable declared with the memory keyword in sortTheArrayOrders function is getting sorted even though it is passed to quickSort function as a memory function parameter. According to my understanding, using memory for the function parameter always creates a new memory allocation and copies the data there. The memory data created during function execution is deleted once execution is done. Then how is it that the memory-array(orders) has actually been modified in the caller function (sortTheArrayOrders)? Does it mean EVM points to the memory address instead of creating another memory space if the memory data is being passed as a memory function parameter?
Code:
contract sortOrders
{
struct D_Order
{
uint price;
}
mapping(uint => mapping(uint => D_Order[])) public orderStateVar;
function sort() public
{
orderStateVar[0][0].push(D_Order(10));
orderStateVar[0][0].push( D_Order(87));
orderStateVar[0][0].push(D_Order(56));
sortTheArrayOrders();
}
function sortTheArrayOrders() internal returns(D_Order[] memory)
{
D_Order[] memory orders = orderStateVar[0][0];
quickSort(orders, int(0), int(orders.length - 1));
}
function quickSort(D_Order[] memory arr, int left, int right) internal
{
//quicksort inplace sorting algorithm
//returns nothing
}
}
- I tried assigning the return value from sortTheArrayOrders to the state variable array: orderStateVar[0][0] inside the sort() function like shown in the below code, but I am facing this error: "UnimplementedFeatureError: Copying of type struct sort.D_Order memory[] memory to storage not yet supported." Is it possible to return the value of type storage from a function? How can I assign the returned value to the state variable?
Code:
function sort() public
{
orderStateVar[0][0] = sortTheArrayOrders();
}
Thanks in advance!