Questions tagged [memory]

Questions related to memory use in Solidity or by nodes

Filter by
Sorted by
Tagged with
0 votes
1 answer
16 views

In My ERC-721 Contract, tokenURI Doesn't Return A String, It Returns A Promise

In my solidity contract, the function for tokenURI looks like this: function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { require( _exists(tokenId), ...
user avatar
0 votes
1 answer
41 views

EVM are stack and memory part of the EVM context

Does anyone knows what happens to the stack and memory of the EVM when an external contract is invoked? Do the stack and memory get "cleaned" in order for the external contract to execute ...
user avatar
0 votes
0 answers
12 views

Why Is My Smart Contract Not Able To Be Optimized?

I have a smart contract that is barely below the limits of the solidity compiler. When I try to enable optimization at 200 runs I get this error: CompilerError: Stack too deep when compiling inline ...
user avatar
  • 113
0 votes
1 answer
11 views

Methods that only return arrays are very common. But, in the 'returns' argument, do I use memory or storage? and why?

// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9; contract MyContract{ address[] public myAddresses; function getMyAddresses() public view returns(address[] [[...
user avatar
  • 3
0 votes
1 answer
21 views

Understanding memory type solidity in function's return type

pragma solidity ^0.8.0; contract Crud { struct Users{ uint id; string name; } Users[] users; // this is type storage uint length = 0; function addUser(string ...
user avatar
0 votes
2 answers
35 views

How to store a large mapping in a smart contract?

I have a contract that need to access the address of different tokens given their name as a string. I thought about doing the below: contract MyContract{ mapping (string => address) public ...
user avatar
0 votes
0 answers
180 views

Using memory vs storage for arrays in structs in Solidity - Q&A

Wanted to share what I learned to help someone in the future running into a similar issue: I have this struct, which includes two arrays. In createNewEvent, I initialize two arrays, confirmedRSVPs and ...
user avatar
0 votes
0 answers
38 views

Solidity dynamic arrays in memory: overflow bug

I was trying to create a lottery smart contract that stores every tickets (ticket = address) that someone buys into into an array to later on select winners from it. Unfortunately I stumbled into an ...
user avatar
  • 1
1 vote
0 answers
12 views

Why cannot insert elements dynamically into 2D memory array

I have some issues with understanding exactly how the arrays and memory works in solidity, but I found this nice question that kind of help me to understand some parts - Solidity 2D in-memory array ...
user avatar
  • 21
0 votes
0 answers
8 views

Efficiency of a Redundant Accounting Variable

Say I have users who all own various amounts of a token in the contract. Each user would have their own struct documenting their current holdings of the token. mapping(address => UserAccount) ...
user avatar
0 votes
2 answers
55 views

How to push elements in an array within a function?

Can anyone help me figure out the issue with this function in the code? function getVerifiedProposals(uint index) public returns (string[] memory, string[][] memory, address, uint[] memory) { ...
user avatar
1 vote
1 answer
85 views

Calling a function with calldata parameter from another contract

Contact A: function redeemAssetsForBuyback(address[] calldata tokens) external { // ... } I would like to call redeemAssetsForBuyback from Contract B: function initiateRedeem() external { // ... ...
user avatar
  • 111
2 votes
0 answers
41 views

Can't find memory field in structLogs field in debug_traceTransaction RPC Call

I'm trying to index internal eth transactions transaction of the ethereum blockchain and as it is not present in the logs, the way is to replay the transaction with debug_tracetransaction so I can ...
user avatar
1 vote
0 answers
105 views

The reason why can't I use calldata as a data location for constructor parameter

I know what is difference between memory and calldata. But seeing that I ask this question I don't seem to understand deeply yet. I ran into an error message as below, when I used 'calldata' as a data ...
user avatar
  • 11
1 vote
2 answers
59 views

Why does this function not update my storage array?

Since reference types such as arrays are passed by reference by in Solidity, they pass a reference (pointer) to the value. Any change to that value will reflect on original value. If this is the case, ...
user avatar
  • 925
0 votes
1 answer
86 views

`SLOAD` on an array, does it call `SLOAD` many times?

Let's say I have an array in storage: // SPDX-License-Identifier: MIT pragma solidity ^0.8.7; contract Test { uint256[] s_array = [1,2,3,4,5,6,7,7,8,8,8,8,8,8,8,8,8,8,8]; function doStuff() ...
user avatar
0 votes
1 answer
31 views

What's the relative cost of reading `msg.value` more than once?

Let's say we have the following payable method defined in a contract: function foo() public payable { if (msg.value >= thresholdValue0) { // some logic.. } else if (msg.value < ...
user avatar
0 votes
1 answer
17 views

return array of size n

I need a function that willl return an array of size n and have each element be a 1. How do I write this in solidity? I need something like uint256[] a = makeOnesArray(5) => [1, 1, 1, 1, 1]
user avatar
1 vote
1 answer
14 views

Memory to memory assignment doesn't point to the same reference

Hey I am trying to understand data location more it is stated in the docs that "Assignments from memory to memory only create references. This means that changes to one memory variable are also ...
user avatar
0 votes
0 answers
14 views

Dynamic array in function

I am trying to write a staking contract for a specific ERC1155 contract. The contract i am interfacing has a batch transfer function that takes an array of token Ids to transfer and an array of their ...
user avatar
0 votes
2 answers
465 views

What is the gas cost to read a member of a global struct?

Say I have a global state variable struct MyStruct that has multiple members/attributes/fields, what is the gas cost for reading a specific member? Is it equivalent to the gas cost of reading the ...
user avatar
  • 230
0 votes
1 answer
63 views

How do I parse a bytes32 in solidty?

I have a bytes32 object in memory or storage and I want to parse it into little chunks, for example into one uint8, followed by a bool, followed by uint10, followed by address. How can one do that? I ...
user avatar
  • 179
0 votes
0 answers
13 views

:Are there reasons for not using calldata for property setters

My doubt stems from Openzeppelin ERC721 constructor: constructor(string memory name_, string memory symbol_) { _name = name_; _symbol = symbol_; } It is my understanding that ...
user avatar
  • 329
1 vote
2 answers
892 views

How to initialize an array inside the function and push items into it?

How to initialize an array inside the function and push items into it? I'm not gonna initialize outside the function. Here's my code: function tokenOfOwner(address owner) public virtual returns (...
user avatar
  • 147
0 votes
1 answer
152 views

What is the "bytes memory" in (bool success, bytes memory) = some_address.call{value: some_amount}("")

I understand that the success tells if the receive/fallback methods returned successfully but what does the memory give?
user avatar
0 votes
2 answers
53 views

What is the disadvantage of allocating a new memory array as a way to dynamically increase its size in Solidity?

Since dynamically increasing in-memory arrays is not allowed in Solidity, I thought about creating a helper method like the below to create a new array with one more element every time I wanted to ...
user avatar
  • 101
1 vote
1 answer
38 views

When to use memory and when not to use?

I watched this video and he uses memory a lot like function verify(address _signer, string memory _message, bytes memory _sig. Why so? Could the function work without using memory? While the usual ...
user avatar
0 votes
1 answer
195 views

How to convert between uint256[] and uint256[x]

Basically, I want to call a function myfunct(uint256[] memory) but I have a uint256[13] I have the uint256[13] because I want to temporarily store uint256s in a function, and save memory--not do ...
user avatar
0 votes
1 answer
106 views

Gas costs of multiple Writes / Reads to same slot in transaction

I know that the EVM wants to charge for reading from slots, and even more for writing to those slots. That all makes sense. My question is, whether the EVM is smart enough to store in memory the ...
user avatar
2 votes
2 answers
137 views

Are storage values copied or referenced?

Given this contract contract C { struct Product { uint price } Product[] storage products; function doSomething() { products.push(Product({price: 100})); products.push(...
user avatar
1 vote
1 answer
166 views

Weird behavior when dealing with uint32 memory array in Yul

There's something I can't understand in the way Solidity handles dynamic-size memory arrays. For instance, let us consider the following contract: pragma solidity ^0.8.0; ...
user avatar
1 vote
1 answer
73 views

Can you use a local `storage` value after it is deleted from a mapping or array?

In Solidity, if you delete a value from a mapping or shorten an array, are local storage references to the deleted item still valid? Case 1: contract Case1 { mapping(uint256 => Widget) private ...
user avatar
1 vote
1 answer
267 views

Why do you have to specify calldata or memory for complex types?

Why do you have to specify calldata or memory for complex types like (string, array, struct etc)?
user avatar
1 vote
1 answer
300 views

What is the layout of returndata from returndatacopy opcode?

I am attempting to perform operations on returned data from delegatecall opcode using a similar familiar proxy pattern: assembly { let ptr := mload(0x40) // load empty memory address pointer ...
user avatar
  • 626
0 votes
1 answer
29 views

default storage location

This post (When should I use calldata and when should I use memory?) explains the differences between calldata and memory. However where are function arguments or variables within functions stored if ...
user avatar
0 votes
1 answer
131 views

Initialising an array with a single value in solidity

I want to create an array of all 1's. As per my understanding, doing it manually after declaration through a for loop will lead to high gas expense. So, is there any way to initialize an array at once,...
user avatar
0 votes
1 answer
39 views

Dynamic memory management in contracts

After reading a ton of solidity and ethereum documentation and following a course about writing ethereum contracts, a question has arised. This is: When a contract is deployed to the blockchain, it ...
user avatar
  • 159
0 votes
0 answers
26 views

How do I use memory?

I am learning solidity, and are trying to get "totalBuyFee" to be constantly reducing each time the function "greenwallbuyfee" is called. Do you guys know how I can retrieve the ...
user avatar
0 votes
0 answers
24 views

Reset array require an additional "(0)"

I'm writing a contract where I need to reset an array (contract attribute) to an empty array: pragma solidity >=0.6.0; contract MyCon { address[] public arr; function test() ...
user avatar
4 votes
2 answers
681 views

Why does Solidity never free memory?

I was reading Solidity's Layout in Memory docs, and I was rather surprised to learn that: Solidity always places new objects at the free memory pointer and memory is never freed (this might change in ...
user avatar
1 vote
1 answer
185 views

Understanding why this blob of assembly code adds 0x20 and 0x1f [duplicate]

The execute function defined in MakerDAO's DSProxy contains this blob of assembly code: assembly { let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0) ...
user avatar
1 vote
1 answer
78 views

Does data in memory variables get added to the blockchain? How does consensus work for memory?

If a function call involves a memory variable, does the data in memory get added to the blockchain history? If no, how come others can validate the transaction? Is the data only deleted after a block ...
user avatar
1 vote
1 answer
171 views

Where are value-type function arguments stored in Solidity?

When we define a function, say test(uint256 val). I would presume val would be implicitly defined in memory. But what if we change the funtion visibility modifier, does that change the location of ...
user avatar
0 votes
1 answer
46 views

Data location memory or calldata [duplicate]

Can someone tell me how to solve this problem? I am not advanced and cannot figure it out.
user avatar
0 votes
1 answer
64 views

Using storage for local function variables

In one function, I was forced to declare a variable with the memory type storage (since there is mapping inside the struct). Is it much more expensive than memory? And how justified is it if I will ...
user avatar
2 votes
0 answers
204 views

Converting static variable to memory

I have the following code snippet in my contract, trying to call flashLoan from Aave. address private constant LINK = 0x...; function myFlashLoanCall(uint256 _amount, bytes memory _params) public {...
user avatar
1 vote
1 answer
39 views

Why 1225312 is the last addressable location in memory?

I was dabbling with inline assembly, and I wanted to discover the extent of addressable memory. After many trials, I found out that the only usable memory locations that could be accessed with OPCODEs ...
user avatar
1 vote
0 answers
22 views

How to give an array in storage value from an array from a function's input parameters

Below is the code that I have: struct Cow{ uint age; uint name; } Cow[] cows; // this is the code I am trying below. function createCows(Cow[] memory _cows) public { cows =_cows; } When ...
user avatar
  • 1,333
1 vote
1 answer
252 views

Data location specification and bytes32

Why is data location not required to be specified for bytes32?
user avatar
1 vote
0 answers
55 views

Saving String Memory Argument to Struct Array

I have the following Solidity code that compiles and tests okay, but the insurancePlan property of the Patient struct is not being saved. I've tried many variations and it is just blank (empty) when I ...
user avatar
  • 11