Understanding Gas Consumption

Here you can ask questions related to this specific course section.

4 Likes

Hi @filip, I am very happy to get started with the new course. I could gain valuable insight watching the Copy vs. Reference video. But when you try to push to a dynamic memory array I got a little confused. Isn’t it that push is only available for dynamic storage arrays? This confuses me a little.

Thanks.

EDIT:

Another thing that confuses me is this:

1 Like

Hey @Bhujanga, hope you are ok.

Accessing an array past its end causes a failing assertion. Methods .push() and .push(value) can be used to append a new element at the end of the array, where .push() appends a zero-initialized element and returns a reference to it.

https://docs.soliditylang.org/en/latest/types.html?highlight=push#arrays

push statement is used to append elements into an array, dynamic or fixed, is a general term for all type of arrays (to append or add elements on it)

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Hi @thecil,

hmm… I am not sure if I understand you right. Reading the documentation does not make it any clearer as it says

push :

Dynamic storage arrays and bytes (not string ) have a member function called push that can be used to append an element at the end of the array. The function returns the new length.

There is no talk about having a push function for dynamic memory arrays. What am I confusing here?

Thanks for your time and patience :pray:

1 Like

I think you are confusing with Data Storage Type, it does not matter if an array is memory or storage, fixed or dynamic, the push function is general for all arrays. (or thats what i’m trying to explain :nerd_face:)

To append (or add) elements into an array (it does not matter its type), you have to use the push function.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Ok I see. So why is this code throwing an error in my contract and it is working with Filip’s? (See video Copy vs. Reference)

:thinking:

1 Like

Hi,

In Copy vs Reference video, Filip you said at 10:52 (memoryArray = pointerArray;) that it is a copy operation. Does it is because the pointerArray is a storage variable because we daclared it like storage in function f or because it is pointing to storageArray which is at the beginning of contract like state variable? Because state variables are stored in a storage.

1 Like

Hey @Bhujanga, hope you are ok.

We have fixed the contract to solve that little issue, if you check filip repository again, the contract has been updated.

Carlos Z

1 Like

I did not understand quite well your question, Its a copy operation because you are assigning all values on pointerArray to memoryArray, but is just a simple example to understand properly the type of data referenced.

Carlos Z

1 Like

Hi @filip, I am trying to rewrite the Copy VS Ref contract, following the tutorial but I am getting an error on line 13, where it’s giving me this error:

TypeError: Member “push” is not available in uint256[] memory outside of storage. --> browser/academy/CopyRef.sol:14:9: | 14 | memoryArray.push(4); | ^^^^^^^^^^^^^^^^

pragma solidity 0.8.0;
// SPDX-License-Identifier: MIT
pragma abicoder v2;

contract C {
    
    uint[] storageArray; // [1,2,3]
    
    //Assign by copy
    //Assign by reference
    
    function f(uint[] memory memoryArray) public { //[1,2,3]
        storageArray = memoryArray; // copy memoryArray to storageArray
        memoryArray.push(4);
    }
}
2 Likes

Hey @bjamRez

That is correct you cannot use the method .push() in a memory array.
You can only do that in storage arrays.
Filip example was just an example to prove a concept :slight_smile:

Cheers,
Dani

4 Likes

in my multi sig wallet i used toooo many arrays i thought that it was cool to have lists for everything :frowning:

Good stuff to know, also is it true that loops use too much gas?? I kept trying to avoid loops as much as possible because i heard that, i bet thatll be in the course later, aaand ooof the ETH 201 was around 2017, filip looks all dfferent now xD

2 Likes

Hey @Carlosvh96

You should definitely avoid arrays as much as you can and only store the info that you really need the most.
For other information you can emit events and fetch them later on using web3.js

Cheers,
Dani

Hi @filip! A question regarding the reference assignment video.
In lower level languages like C, pointers are used to dynamically allocate memory, sometimes for performance reasons or sometimes because it is the only way to achieve some types of data handling.
My question is, what is the use case to have pointers in a program/smart contract that runs on EVM?

Thanks :slight_smile:

Hey @karips

A really good lecture about pointers in Solidity, check it out :slight_smile:

https://blog.b9lab.com/storage-pointers-in-solidity-7dcfaa536089

Happy learning,
Dani

1 Like

Hi @dan-i. Thanks a lot! Excellent read that raises awareness about the possible pitfalls of using pointers. It seems that the newer compilers are trying to help the devs from shooting themselves in the foot, but one should be aware of the dangers nonetheless.

1 Like

Hi, so in Solidity 101 in Multisig wallet project we saved our transfer requests in struct array, which was in storage. Which is costly.

Is it possible to emit event and then work with data from the event log instead of a struct array?

1 Like

Sure you can, once you emit events you will be able to fetch this data using web3.js

2 Likes

Hi Daniele,

Ok, so .push() can only be done in for storage array.

But in the video, Filip’s Remix does not show a red colour error when he entered the code. Why is that so ?

Cheers,

Can you please elaborate your question?
What’s the piece of code you are talking about, what did Filip was trying to explain?

Cheers,
Dani