Questions tagged [memory]
Questions related to memory use in Solidity or by nodes
206
questions
143
votes
2
answers
63k
views
When should I use calldata and when should I use memory?
I have seen people use both memory and calldata keywords when writing Solidity. Specifically, they are used when declaring function parameters that take dynamic types like 'structs' or 'arrays'.
When ...
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.
...
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!
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/...
33
votes
3
answers
24k
views
Can I save structs in a Mapping?
Is it possible to store structs in a Mapping or any dynamic memory structure in my Solidity SmartContract?
33
votes
3
answers
40k
views
TypeError: Data location must be "storage" or "memory" for parameter in function, but none was given
/// @dev Adds a new transaction to the transaction mapping, if transaction does not exist yet.
/// @param destination Transaction target address.
/// @param value Transaction ether value.
/// @param ...
25
votes
4
answers
26k
views
How to fill dynamic in-memory array
For some reason solidity doesn't allow to push values into memory array
Member "push" is not available in bytes32[] memory outside of
storage.
Here is sample contract code:
pragma solidity ^0.4....
18
votes
2
answers
12k
views
How much data can I store in a smart contract, what is the cost and how it is implemented?
If I suppose I use a smart contract as a database, how much data can I store?
speaking only of strings and integers, save them in contract data structures does it cost in addition to the gas used for ...
16
votes
3
answers
19k
views
How to initialize an empty array inside a struct?
Is there a mistake in the way I initialize Bar b in the function foobar?
contract Foo {
struct Bar {
address owner;
uint[] x;
}
Bar[] public bars;
function foobar(...
14
votes
2
answers
3k
views
error: Storage location can only be given for array or struct types
Getting the error "Storage location can only be given for array or struct types" in a setup function. The exact line is
uint memory _total = 0;
I'm confused by the error, because I'm not trying to ...
13
votes
3
answers
6k
views
How to run geth at 512Mb RAM Digital Ocean droplet?
When geth process consumes too much memory it gets killed (not at all gracefully) by Digital Ocean.
Partly solved the problem by implementing swap - Setting up geth Ethereum node to run ...
11
votes
2
answers
38k
views
The difference between contract address and wallet address
Disclaimer: this is a rather long question.
To my understanding, contract address and wallet address are two profoundly different concepts, with the only thing in common being the fact that they are ...
11
votes
3
answers
2k
views
Why memory arrays are unresizable?
I don't know the required length of my array before execution of the function.. why technically can't I resize my memory array?
What is the common way to go around of this problem, if you are also ...
11
votes
1
answer
3k
views
Does "delete" on mapping really frees the memory space?
I have the following mapping(uint => Foo[]) data structure. I have mapped 10 unique ids with 10 Foo structs, where each Foo is an arraylist of 10 items.
Step 1:
for(int i = 0; i < 10; i++)
...
10
votes
2
answers
5k
views
Invalid array length, expected integer literal
I'm trying to declare an in-memory array with a fixed length:
uint length = 10;
uint[length] memory priorityList;
However, it just displays this error message:
Invalid array length, expected ...
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 ...
8
votes
2
answers
3k
views
How to pop from (decrease the length of) a memory array in Solidity
I have a function that gets an address array from another contract, conditionally deletes the msg.sender from the array, then saves the new array back to that contract.
Because the array backerList ...
8
votes
1
answer
996
views
What is the composition of the blockchain database?
From axic's explanation of the structure of the ethereum database, we learned that data is stored in Merkle Patricia tries, usually inside a LevelDB store. It consists of structures such as blocks, ...
7
votes
2
answers
2k
views
Why does Solidity never free memory?
I was reading Solidity's Layout in Memory docs, and I was rather surprised to learn that:
Solidity always places new objects at the free memory pointer and memory is never freed (this might change in ...
6
votes
3
answers
781
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 ...
6
votes
1
answer
3k
views
Is it possible to decrease RAM usage of go-ethereum(geth)?
I have a slow computer, which has 2GB memory RAM. One geth app uses %60 percent of the RAM, which lead me to run only one geth node; I cannot run 2 geth nodes in parallel due to RAM overload.
[Q] Is ...
6
votes
1
answer
684
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 ...
6
votes
2
answers
4k
views
Assigning memory variable to storage variable, what happens?
I'm curious what happens if you try to do the next thing:
contract C {
Struct S {
uint a;
uint b;
}
mapping(address => s) structs;
function updateStructs(S ...
6
votes
1
answer
2k
views
In solidity, how can I hardcode bytes > 32 bytes
I want to create a contract with a hardcoded bytes variable.
contract Test(){
function Test(){
bytes x = 0x..... //greater than 32 bytes
}
}
The above returns a Type int_const not ...
6
votes
1
answer
7k
views
Where are strings stored in Solidity?
I wonder where strings are stored in Solidity?
Especially, where are string literals, function arguments, and variables stored?
I recall, I've read that strings cannot be saved to memory, but is ...
6
votes
2
answers
3k
views
Assigning an array creates a pointer or copies an array? (Solidity)
Here is the code:
pragma solidity^0.4.16;
contract Test {
struct SomeData {
address[] users;
}
address[] nullArray;
mapping(uint => SomeData) SomeDataList;
function ...
6
votes
1
answer
5k
views
Type struct Mycontract.User memory is not implicitly convertible to expected type struct Mycontract.User storage pointer
pragma solidity >=0.5.0 <0.7.0;
contract Mycontract{
uint256 a;
struct User
{ string name; }
User[] users;
function foo() public{
**User storage user=User("hey&...
6
votes
1
answer
256
views
Could more than one mapping's key point to the same memory space?
Since memory is already allocated on (array[0] = value;), I just want to change its pointing key value. Such as array[1] will allow me to access value now.
Is it possible to change the key of ...
5
votes
2
answers
1k
views
Stack too deep | Use memory array to store many "local" variables?
I have a contract that's doing a fairly complex calculation with some intermediate calculations, and I'm running into the "stack too deep" problem.
I've tried to use a memory array within the ...
5
votes
1
answer
3k
views
Delete an entry in a mapping
To my understanding, delete x merely sets the value of x to the default value defined for the type of x (which is typically 0 in one way or another).
For example:
uint x = 1;
delete x; // sets x = 0
...
4
votes
1
answer
847
views
Why does PUSH1 0x60 PUSH1 x40 MSTORE store 0x60 at memory location 0x50 (and not 0x40)?
According to browser solidity, after executing
000 PUSH1 60
002 PUSH1 40
004 MSTORE
Memory is:
0x00: 00000000000000000000000000000000 ????????????????
0x10: 00000000000000000000000000000000 ?????...
4
votes
1
answer
6k
views
Efficient Bit Packing
I have a struct with the following variables:
struct contract {
address customer;
uint8 tokentype; // 5 token types
uint8 size; // 5 sizes
bool gender; // 2 types
...
4
votes
1
answer
558
views
How does this assembly code create a new "memory end"?
From the docs:
mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f)))
Context:
library GetCode {
function at(address _addr) public view returns (bytes memory o_code) {
...
4
votes
3
answers
1k
views
Should I use `storage` or `memory` when just reading the value and not changing state?
pragma solidity 0.4.24;
contract Test {
struct Book {
uint id;
}
Book[] books;
uint public status;
constructor() public {
books.push(Book(10));
}
...
4
votes
1
answer
4k
views
Creating a 2D in-memory array in Solidity
I'm trying to work with 2D in-memory arrays in Solidity, in which the size is not known at compile time. I've noticed that if I define the array size as a constant, then I can use this syntax, which ...
4
votes
1
answer
2k
views
In solidity, how to copy a string or pass it by value?
In this example I only modify string a, but both a and b are changed. It seems like the string wasn't copied. I only want to change a. How do you copy a string and how do you pass a string by value to ...
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 ...
4
votes
1
answer
134
views
What happens when memory is full in a node?
I am using go-ethereum and running a private blockchain network. I want to what is supposed to happen when the memory is full in the nodes. Will the program stop abruptly or will the old blocks get ...
4
votes
1
answer
3k
views
How do mappings in solidity occupy memory
Say, I declare a mapping that uses uint256 numbers as keys and maps them to a single 8 bit character. I add 5 members to this mapping.
Obviously, I can then access any of these 5 characters using ...
4
votes
1
answer
1k
views
How to return dynamic sized array's original address on the memory of Solidity?
On the following source code, memOffset returns local values such as 96, 128 as the address value. How could I make it return the original address that is located on the memory, is it possible? After ...
4
votes
1
answer
945
views
Clarification on how Calldata persists on the blockchain and how Optimistic Rollups use it
So I see that a lot of optimistic rollup solutions say that they store the chain's transaction data on Ethereum via "calldata" which is way cheaper in gas costs than contract storage (I ...
4
votes
1
answer
277
views
Is it possible to get an address of a struct in the Library or Contract without using mapping?
[Q] I was wondering is it possible to get an address of a struct on the memory space? Inside initialise() function after the Node object creation, I want to get is address if it is possible. Since ...
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 ...
4
votes
3
answers
640
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])) ...
4
votes
1
answer
393
views
personal.newAccount("mypassword") stops the server when i create two accounts consecutively in local privatenet
personal.newAccount("mypassword")
stops the server when i try to create two accounts one after the another.
personal.unlockAccount(eth.accounts[0],"mypassword");
same happens when I try unlock ...
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 (...
3
votes
1
answer
1k
views
Why use anything but uint256/int256 outside of a struct?
I see how uint8 can save space within a struct, but outside of that I do not see any reason to use anything but uint256. The gas cost is slightly higher for uint8. The only reason I can think of is ...
3
votes
1
answer
688
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 ...
3
votes
1
answer
138
views
Are balances of all accounts in memory or disk (storage)?
I simulated two contracts that sending balance to another and simply setting value of a variable(var a = 5;). In contrast my expectation, {var a = 5;} contract consumed more gas than the other.
In my ...
3
votes
2
answers
2k
views
Is it possible to copy the internal storage of a smart contract to a new contract?
Assume that I want to implement my own version of a suicide contract function with extended functionality. More specifically, I want to copy the internal storage state of the contract to a new ...