0

For example, when a struct is initialised to storage in a function, then assigned to a global state variable such as an array or mapping type, does the local struct get copied to the global state variable, or does the global state variable reference the local struct?

The solidity docs say that local storage variables can reference global state, but it doesn't specify if it works both ways?? E.g.

contract C {
    struct S {
        uint n;
    }

    // Global state mapping:
    mapping (uint => S) m;

    // Is data copied or referenced from the local storage object in the function to global storage??
    function localToGobal(uint i) public {
        S storage obj = S(i);
        m[i] = obj;
    }

1 Answer 1

0

Does the global state variable reference the local struct?

That is never the case in any programming language (Solidity included), because during runtime, local variables are not viable (do not deterministically exist within memory) once the execution of the scope in which they are declared (a function in your case) completes.

Your Answer

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

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