Here you can ask questions related to this specific course section.
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:
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!
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
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 )
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!
Carlos Z.
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.
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
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
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);
}
}
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
Cheers,
Dani
in my multi sig wallet i used toooo many arrays i thought that it was cool to have lists for everything
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
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
Hey @karips
A really good lecture about pointers in Solidity, check it out
https://blog.b9lab.com/storage-pointers-in-solidity-7dcfaa536089
Happy learning,
Dani
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.
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?
Sure you can, once you emit events you will be able to fetch this data using web3.js
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