Questions tagged [memory]
Questions related to memory use in Solidity or by nodes
206
questions
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
1
answer
14
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 = ...
0
votes
1
answer
504
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,...
2
votes
1
answer
270
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 ...
4
votes
3
answers
642
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])) ...
2
votes
1
answer
50
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
2
answers
360
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, ...
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 ...
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 {
// ...
...
1
vote
3
answers
482
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
103
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
33
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
36
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
0
answers
41
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
141
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
783
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
81
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
44
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[] ...
0
votes
0
answers
56
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 ...
2
votes
1
answer
37
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
67
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
245
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
18
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
187
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
107
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
19
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
209
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
72
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
249
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
234
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.
0
votes
1
answer
468
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
43
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
322
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
32
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
308
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
205
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
82
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 ...
2
votes
2
answers
145
views
why a parameter must be "storage" or "memory"?
I am creating a token like this:
pragma solidity ^0.5.0;
import "../utils/math/SafeMath.sol";
contract ExampleDivisibleNFTs {
using SafeMath for uint256;
// Percentage of ownership over a ...
0
votes
1
answer
71
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 ...
34
votes
7
answers
16k
views
How to store IPFS hash using bytes32?
Following Q/A (What datatype should I use for an IPFS address hash?) recommend us to use bytes to store IPFS hash.
I was using following example(https://github.com/AdrianClv/ethereum-ipfs/blob/master/...
1
vote
3
answers
110
views
How do programs on the 'Ethereum World Computer' compare to traditional programs or websites?
So this question has been bugging for me for a while and I can't seem to find the answer to it.
Context
I have been a web developer for many years.
When I access a website, any frontend code is run ...
3
votes
1
answer
691
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 ...
0
votes
1
answer
72
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) { //...
1
vote
1
answer
255
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() ...
6
votes
1
answer
686
views
How does dynamic array memory reallocating work?
Let's say I have a dynamic array of some length and I want to push a new element, but memory after last elem is already used.
What would happen in such case? Compiler would find a new 'chunk' of ...