I would like to know if it is considered good practice initializing a struct when instantiating it or if it is just equally good to initialize its content right after instantiating it.

In other words, if this two ways of instantiating "p" are the same (from a bytecode point of view)

struct Player{
    address id;
    uint256 balance;
// OPTION 1: initializing when instantiating
Player memory p = Player(0xHJ76...uy7, 100); 
// OPTION 2: initializing after instantiating
Player memory p;
p.id = 0xHJ76...uy7;
p.balance = 100;

To me the are but I might be missing something

1 Answer 1


Both methods achieve the same thing but use a different approach, so the difference is only in gas efficiency.

In my test, the first method used 21360 gas, whereas the second 21382 gas.

To answer your question they are not the same, they use different OPcodes.

It seems counterintuitive that separate initialization and instantiation are more efficient than having them combined, but EVM often is.

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.