Questions tagged [memory]

Questions related to memory use in Solidity or by nodes

Filter by
Sorted by
Tagged with
0 votes
0 answers
15 views

Is it safe to reuse same memory slots for both inputs and ouputs when working with assembly call?

Is it safe to reuse memory for both inputs and ouputs when working with assembly call? If not, what can go wrong? Contract below uses assembly to call Target contract YulStaticCall { function ...
tsknakamura's user avatar
0 votes
2 answers
20 views

When should I choose lower-size bytes variables (e.g. bytes23) over bytes32 variables?

I am trying to manage strings on a smart contract. I know these strings will be 46 bytes long so I thought I could convert them into bytes and split them in two bytes32 arrays. Then I realized smaller ...
Pietro D'Amico's user avatar
0 votes
1 answer
9 views

Why must new arrays in Solidity be created with the new type[](array_length) notation?

I have the following piece of code in which I try to re-set the funders array to an empty array. contract FundMe { address[] public funders; function withdraw() public { for (uint i=...
akayuyang's user avatar
1 vote
0 answers
28 views

Force geth to periodically save state to disk

I'm running a geth node with lighthouse, and I'm having an issue where geth will periodically crash due to being out of memory. My system has 16 GB of RAM, and I'm not sure what the underlying issue ...
user6013's user avatar
  • 111
1 vote
1 answer
51 views

Creating a local memory copy of a global uint256

I'm struggling to find any documentation that describes how to create a local memory copy of a global uint256 variable. Consider the following example: contract MyContract { uint256 myInt = 0; ...
jambolina's user avatar
0 votes
1 answer
17 views

Copying memory[] to storage not supported on initialization

I have a data structure where: The smart contract holds an array of structs (a "community") in state That struct has an array of structs ("delegates") When I try initialize one ...
RanierResearch's user avatar
1 vote
2 answers
51 views

How are bytes and bytes32 different in inline-assembly?

function mloadTest1() public pure returns(bytes memory) { bytes memory a; assembly { a :=mload(0x40) } return a; // 0x20 } function mloadTest2() public pure returns(bytes32) { ...
imelon2's user avatar
  • 528
1 vote
1 answer
24 views

Inline assembly loop doesn't update memory

I want to copy in inline assembly from calldata the last two bytes of every 32 byte slices to the lower half of the first free memory slot(32 bytes). free = free memory pointer + 14 (bytes) moffset = ...
Igor József Vass's user avatar
3 votes
1 answer
512 views

What is a returnbomb attack?

I've read that when making an external call to an unknown contract, if the contract returns a large amount of random or junk byte it will cause some type of bug. But I dont know exact what it is and ...
kumar's user avatar
  • 61
2 votes
1 answer
107 views

Why can you slice bytes in calldata but not in memory?

If trying to slice bytes in memory one gets the error below. The solidity docs give no explanation just state that array slices are only supported for calldata as of now. TypeError: Index range access ...
FreddyC's user avatar
  • 301
0 votes
1 answer
207 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,516
0 votes
0 answers
48 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
15 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
44 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
230 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
105 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....
Zommuter's user avatar
  • 113
0 votes
1 answer
47 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
2 votes
1 answer
45 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
48 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
102 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
  • 323
0 votes
0 answers
371 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
20 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
  • 338
0 votes
1 answer
254 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
110 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
  • 301
1 vote
1 answer
24 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
  • 301
0 votes
1 answer
302 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
  • 638
1 vote
2 answers
310 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
  • 338
0 votes
2 answers
66 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
1 vote
1 answer
451 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
  • 957
0 votes
1 answer
35 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
273 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
  • 957
1 vote
1 answer
97 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
87 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,260
0 votes
1 answer
74 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
229 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
257 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
  • 239
0 votes
1 answer
350 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
502 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
40 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
257 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
394 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
1 answer
2k 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
309 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
77 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 ...
VitoCK's user avatar
  • 161
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
3 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
  • 131
2 votes
1 answer
360 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
760 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
403 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,654

1
2 3 4 5