Skip to main content

Questions tagged [memory]

Questions related to memory use in Solidity or by nodes

Filter by
Sorted by
Tagged with
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 ...
1 vote
2 answers
425 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, ...
0 votes
1 answer
29 views

Invalid type for argument in function call. Invalid implicit conversion from literal_string "" to bytes calldata requested

I'm doing the course of Patrick Collins to learn about programming in Solidity; but some libraries from Chainlink has been changed since 2 years ago. So, when I try to compile the code I have the next ...
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 { // ... ...
0 votes
1 answer
31 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 ...
4 votes
2 answers
2k views

Declaring a memory array of storage pointers in Solidity

I would like to declare a variable as a memory array of pointers to storage arrays of uint256. For example: pragma solidity ^0.4.24; contract A { uint256[] public array0; uint256[] public ...
0 votes
2 answers
21 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
689 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,...
0 votes
1 answer
10 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=...
40 votes
3 answers
15k views

Difference between Memory and Storage?

What is the difference between Memory and Storage ? From both the EVM point of view and the contract design one. Thanks!
4 votes
3 answers
676 views

convert bytes to uint8[8] using mstore()

I know how to convert bytes to uint8[8] using mload but mstore seemed like it could be more gas efficient. Is something like this possible? function bytesToUintArray(bytes input) returns (uint8[8])) ...
1 vote
0 answers
29 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
52 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
19 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
1 answer
12k views

GPU'S Failed cannot create DAG

Last night everything is fine and then this morning I check my mining its's closed I think it was restarted or close accidentally then when I open my pool I have this error. ETH: 5 pools are ...
1 vote
2 answers
54 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
25 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
628 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
118 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 ...
1 vote
3 answers
502 views

Where are local variables of Primitive (Elementary) data types stored in Solidity

Though in the latest solidity compiler versions, it has been made clear that for reference variables local to a function, one needs to now explicitly mention its location modifier (memory, storage), ...
0 votes
1 answer
264 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
53 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.
1 vote
1 answer
50 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 ...
1 vote
1 answer
243 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;...
6 votes
3 answers
835 views

Variable Names and Memory

Is there a cut-off where a variable name uses more memory? For instance: uint public priceOfEthYesterday9AMUSAET vs uint public pEth One is more descriptive, but if it's costing me gas I don't ...
0 votes
2 answers
133 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....
1 vote
1 answer
4k views

Memory usage and limits for go-ethereum

geth (and swarm) seem to require a lot of memory, and there are also reports that they tend to an all you can eat approach to system limits. 1) What are the actual memory requirements for geth? 2) ...
0 votes
1 answer
50 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
46 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 ...
0 votes
1 answer
121 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?
4 votes
1 answer
3k views

Switching from 'storage' to 'memory' increases the gas-cost

I've read several related answers and blog posts, all of them suggesting that for the sake of reading complex state variables (i.e., arrays and structures), one should always prefer declaring the ...
0 votes
0 answers
469 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
21 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
302 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
113 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 ...
56 votes
2 answers
26k views

What does the keyword "memory" do exactly?

I've been looking through the code of Etherdice and noticed that some variables are declared like ParserResult memory result; and I haven't found the keyword "memory" in any documentation. ...
0 votes
1 answer
362 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
1 answer
79 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 ...
1 vote
2 answers
354 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 ...
3 votes
2 answers
312 views

How to get live mempool transactions whitout full blokchain

I just want to analyse all mempool txs in realtime. I cant use an api because after several requests my ip gets blocked for some minutes, so I dont get the txs in this time. Thank you for help.
1 vote
1 answer
549 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 ...
0 votes
2 answers
70 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){ ...
10 votes
2 answers
4k views

memory keyword in function argument

What does the memory keyword in the argument of a function do? pragma solidity 0.4.24; pragma experimental "ABIEncoderV2"; contract Conditional { struct Condition { address to; bytes4 ...
1 vote
1 answer
491 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 ...
2 votes
1 answer
378 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 ...
0 votes
1 answer
313 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
104 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 ...

1
2 3 4 5