Questions tagged [memory]

Questions related to memory use in Solidity or by nodes

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

Struct padding, size, and abi.encode

I have the following structure and want to compute its hash: struct SomeStruct { uint8 field1; int24 field2; uint256 field3; } I was expecting that due to packing the size of this ...
kfx's user avatar
  • 1,316
0 votes
0 answers
20 views

Why "It's better to use the bytes type instead of the bytes1[] type"? Solidity

I read a Note in documentation of Solidity: The type bytes1[] is an array of bytes, but due to padding rules, it wastes 31 bytes of space for each element (except in storage). It is better to use the ...
Trong-Hieu Nguyen's user avatar
2 votes
1 answer
14 views

Can you please explain this concept and how it works?

I would like to clear my doubt that when caller and called contract are same. Then the memory will get a new initialization right? or is it a different case.
Mayank Jamwal's user avatar
0 votes
0 answers
39 views

How would look like calldata for bytes[] calldata argument?

Imagine I have a function with such signature in Solidity: function doThing(bytes[] calldata arr) external; how are elements of arr are packed in bytes? I would say that first 4 bytes is bytes4(...
gabbi's user avatar
  • 1
1 vote
1 answer
78 views

Dynamic arrays in memory layout

I think i've figured out dynamic memory layout for dynamic arrays, but could use a check on my reasoning. Consider the contract : pragma solidity >=0.8.4; struct state_type { bytes b;...
Bobo's user avatar
  • 21
0 votes
2 answers
60 views

Can the EVM execute code from memory?

To my understanding address(addr).code and memory[...] are in different places and since code memory is read-only, a usual call doesn't seem to be able to achieve this, but is there any other way to e....
Tobias Kienzler's user avatar
0 votes
1 answer
35 views

Changing storage ref to storage pointer

In the function registerUser below, I am trying to access a global array of structs, and seeing if the argument I have passed exists in the global array or not. The code is as follows: user_details[] ...
Dhairya Khale's user avatar
0 votes
0 answers
44 views

How does EVM handle when passing a memory type variable as a function parameter of type memory?

There are two things that are bugging me about the below smart contract: In the below contract, I noticed that the orders variable declared with the memory keyword in sortTheArrayOrders function is ...
Bobtheblobber's user avatar
2 votes
1 answer
36 views

Best practices for working around the lack of dynamically sized memory arrays in solidity

Consider the following solidity function: function example(uint256[] calldata _numbers) external { uint256[] memory _evenNumbers; // initialize `_evenNumbers` and populate it with even ...
Sam's user avatar
  • 103
1 vote
1 answer
32 views

How do RETURN and MLOAD work for high offsets?

When trying to load the 32 bytes of memory that follow a high offset (say 0x12345678), the RETURN and MLOAD opcodes give an out of gas error. Why is that ? Example PUSH1 0x20 PUSH4 0x12345678 RETURN ...
Sileo's user avatar
  • 139
0 votes
1 answer
50 views

What is the purpose of dynamic memory arrays?

Dynamic memory arrays have a fixed size. What is the purpose of using a dynamic memory array vs. a fixed-size memory array? What makes them dynamic?
Danny B's user avatar
  • 313
0 votes
0 answers
163 views

Maximum amount of state vars, smart contract storage limit and scalable architecture in Solidity

While architecting a smart contract system, which is expected to grow to a large size and be highly scalable, the question arose to use a central module with storage and functionality set in one place ...
Whytecrowe's user avatar
0 votes
0 answers
16 views

string storage vs memory

i have a function in my smart contract which needs a string to work, and for this i have two options : OPTION 1 : Store my string in a state variable : contract A { string public immutable ...
noro meb's user avatar
  • 328
0 votes
1 answer
148 views

Copying of type struct '' memory to storage not yet supported

I can't figure out how to initial this data for the life of me. I have 2 structs which looks like so: struct UserVotes { address userAddress; VoteOptionAmounts[] votes; } struct VoteInfo { ...
Ritzy Dev's user avatar
  • 271
1 vote
1 answer
100 views

Does it make any sense to specify reference type function input parameters as calldata if the given function is only called contract to contract?

As far as I am aware contract to contract calls store input arguments in memory. Or put more precisely they store them in memory and they can be accessed with CALLDATALOAD within the the new call ...
FreddyC's user avatar
  • 151
1 vote
1 answer
18 views

What are the advantages/disatvantages to specify a struct param in a function as calldata vs. memory?

If I am not modifying the input parameter in the function body are there any reasons a reference type parameter should be labeled as stored in memory? More generally what are the reasons to go for ...
FreddyC's user avatar
  • 151
0 votes
1 answer
148 views

Can gas refunds for deleted storage be used as transient storage?

I know there are previous answers around this, but things have changed new EIP's: Right now I'm using an array of structs, for something I only do in memory. It's very messy code and would be much ...
Ryan Sea's user avatar
  • 598
1 vote
2 answers
203 views

Define a struct in library vs in the contract

I have a struct defined in a library (to call it from different contracts): library DataTypes { struct Test{ uint256 a; uint256 b; } when i want to initialize it or update in my ...
noro meb's user avatar
  • 328
0 votes
2 answers
31 views

I'm trying to reverse an array but the transaction keeps getting reverted even though the logic seems right. Can someone help me out?

Here is the code : // SPDX-License-Identifier: MIT pragma solidity >=0.5.0 <0.9.0; contract Day1 { function reverseArray(uint[] memory arr1, uint length) public pure returns(uint[] memory){ ...
GimmeGinNtonic's user avatar
0 votes
1 answer
266 views

Does it cost more gas to store memory local memory variables of a function into one struct or to use scoping?

Digging into aaveV2 and uniswapV2 code recently, and I've notice that the two projects solve the stack too deep error problem is different ways. AaveV2 combines all variables into a struct, and the ...
Bruce's user avatar
  • 867
0 votes
1 answer
31 views

Why is memory used by RETURN misaligned compared to how it was allocated?

I am experimenting with evmone and evmc to execute some simple assembly code snippets on EVM. I wrote a test that is supposed to return 0x42 as output data. The code goes as follows: PUSH 0x42 PUSH ...
radrow's user avatar
  • 103
0 votes
1 answer
158 views

What is the "solidity scratch pad"?

I'm reading up on upgradable proxies, came across this in openzeppelin. //solium-disable-next-line assembly { // Copy msg.data. We take full control of memory in this inline assembly ...
Bruce's user avatar
  • 867
1 vote
1 answer
74 views

What does keyword memory in function return means

In some contexts, the keyword memory seems quite similar to a reference type in C++, where it gives an alias to a variable (see method 1 below). It confuses me when it's used to specify the return ...
Yongkin's user avatar
  • 13
0 votes
1 answer
57 views

Getting Invalid Opcode When I to populate dynamic array [duplicate]

I am writing tests with Foundry and trying to populate a dynamic array. When I try: IAsset[] memory assets; uint256[] memory maxAmountsIn; assets.push(OmniUSD); I get an error ...
0xsegfault's user avatar
  • 1,230
0 votes
1 answer
66 views

Parameter function in Solidity

I am a newbie in Solidity. Below is the snippet of code I came across :- pragma solidity ^0.4.0; contract C { //1 function f(uint len) { //...
Brijesh's user avatar
  • 43
3 votes
1 answer
181 views

why is preloading uint256 array into memory for a for loop faster?

sload loads a word from storage. https://ethereum.github.io/yellowpaper/paper.pdf if we have a loop over uint256, both preloading into memory and loading within the loop should use the same number ...
kirill_igum's user avatar
1 vote
1 answer
192 views

How to store a uint[] memory inside a struct in solidity

I have the below code struct Ticket { uint256 id; uint256[] numbers; address payable owner; } function enter(uint256[] memory userNumbers) public { Ticket memory ...
momoja's user avatar
  • 219
0 votes
1 answer
306 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), ...
ChristianOConnor's user avatar
0 votes
1 answer
446 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 ...
Alberto Molina's user avatar
1 vote
0 answers
33 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 ...
Jim's user avatar
  • 173
0 votes
1 answer
13 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[] [[...
Amit N's user avatar
  • 3
1 vote
1 answer
227 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 ...
Rajan Lagah's user avatar
0 votes
2 answers
321 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 ...
moonrekter's user avatar
1 vote
0 answers
1k 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 ...
camcam's user avatar
  • 11
0 votes
0 answers
283 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 ...
Puddi's user avatar
  • 1
1 vote
1 answer
70 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 ...
Vitomir's user avatar
  • 151
0 votes
2 answers
1k 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) { ...
Bigbunny's user avatar
2 votes
1 answer
2k 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 { // ... ...
CeamKrier's user avatar
  • 121
2 votes
1 answer
263 views

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

I'm trying to index internal eth transactions transactions 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 ...
Cyriaque Pin's user avatar
3 votes
1 answer
630 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 ...
Jamie's user avatar
  • 31
1 vote
2 answers
327 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, ...
Emrah's user avatar
  • 1,524
1 vote
1 answer
243 views

Loading an array into memory, 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() ...
Patrick Collins's user avatar
0 votes
1 answer
148 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 < ...
Ahmed Ihsan Tawfeeq's user avatar
0 votes
1 answer
26 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]
degendeveloper's user avatar
1 vote
1 answer
63 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 ...
jmsandiegoo's user avatar
1 vote
2 answers
2k 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 ...
MShakeG's user avatar
  • 1,064
0 votes
1 answer
377 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 ...
Marlo's user avatar
  • 305
3 votes
2 answers
3k 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 (...
leojail's user avatar
  • 167
0 votes
1 answer
519 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?
tythereum's user avatar
0 votes
2 answers
111 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 ...
Chuck C.'s user avatar
  • 101

1
2 3 4 5