Questions tagged [memory]
Questions related to memory use in Solidity or by nodes
212
questions
0
votes
0
answers
9
views
With assembly call, is it safe to overwrite memory that were used for inputs with outputs?
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 ...
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 ...
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=...
1
vote
0
answers
27
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 ...
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;
...
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 ...
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) {
...
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 = ...
3
votes
1
answer
509
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 ...
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 ...
0
votes
1
answer
206
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 ...
0
votes
0
answers
47
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 ...
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.
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(...
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;...
0
votes
2
answers
104
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....
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[] ...
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 ...
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
...
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?
0
votes
0
answers
369
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 ...
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 ...
0
votes
1
answer
253
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 {
...
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 ...
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 ...
0
votes
1
answer
300
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 ...
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 ...
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){
...
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 ...
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 ...
0
votes
1
answer
270
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
...
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 ...
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 ...
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) { //...
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 ...
1
vote
1
answer
255
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 ...
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),
...
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 ...
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 ...
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[] [[...
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 ...
0
votes
2
answers
392
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 ...
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 ...
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 ...
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 ...
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) {
...
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 {
// ...
...
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 ...
3
votes
1
answer
759
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 ...
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, ...