Questions tagged [memory]
Questions related to memory use in Solidity or by nodes
206
questions
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 (...
0
votes
1
answer
572
views
What is the "bytes memory" in (bool success, bytes memory) = some_address.call{value: some_amount}("")
I understand that the success tells if the receive/fallback methods returned successfully but what does the memory give?
0
votes
2
answers
118
views
What is the disadvantage of allocating a new memory array as a way to dynamically increase its size in Solidity?
Since dynamically increasing in-memory arrays is not allowed in Solidity, I thought about creating a helper method like the below to create a new array with one more element every time I wanted to ...
1
vote
1
answer
60
views
When to use memory and when not to use?
I watched this video and he uses memory a lot like function verify(address _signer, string memory _message, bytes memory _sig. Why so? Could the function work without using memory?
While the usual ...
0
votes
1
answer
698
views
How to convert between uint256[] and uint256[x]
Basically, I want to call a function myfunct(uint256[] memory) but I have a uint256[13]
I have the uint256[13] because I want to temporarily store uint256s in a function, and save memory--not do ...
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 ...
2
votes
2
answers
155
views
Are storage values copied or referenced?
Given this contract
contract C {
struct Product { uint price }
Product[] storage products;
function doSomething() {
products.push(Product({price: 100}));
products.push(...
1
vote
1
answer
569
views
Weird behavior when dealing with uint32 memory array in Yul
There's something I can't understand in the way Solidity handles dynamic-size memory arrays. For instance, let us consider the following contract:
pragma solidity ^0.8.0; ...
1
vote
1
answer
143
views
Can you use a local `storage` value after it is deleted from a mapping or array?
In Solidity, if you delete a value from a mapping or shorten an array, are local storage references to the deleted item still valid?
Case 1:
contract Case1 {
mapping(uint256 => Widget) private ...
1
vote
1
answer
593
views
Why do you have to specify calldata or memory for complex types?
Why do you have to specify calldata or memory for complex types like (string, array, struct etc)?
1
vote
1
answer
1k
views
What is the layout of returndata from returndatacopy opcode?
I am attempting to perform operations on returned data from delegatecall opcode using a similar familiar proxy pattern:
assembly {
let ptr := mload(0x40) // load empty memory address pointer
...
1
vote
1
answer
81
views
default storage location
This post (When should I use calldata and when should I use memory?) explains the differences between calldata and memory. However where are function arguments or variables within functions stored if ...
0
votes
1
answer
503
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
84
views
Dynamic memory management in contracts
After reading a ton of solidity and ethereum documentation and following a course about writing ethereum contracts, a question has arised. This is:
When a contract is deployed to the blockchain, it ...
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 ...
1
vote
1
answer
760
views
Understanding why this blob of assembly code adds 0x20 and 0x1f [duplicate]
The execute function defined in MakerDAO's DSProxy contains this blob of assembly code:
assembly {
let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0)
...
1
vote
1
answer
201
views
Does data in memory variables get added to the blockchain? How does consensus work for memory?
If a function call involves a memory variable, does the data in memory get added to the blockchain history?
If no, how come others can validate the transaction? Is the data only deleted after a block ...
1
vote
1
answer
497
views
Where are value-type function arguments stored in Solidity?
When we define a function, say test(uint256 val). I would presume val would be implicitly defined in memory. But what if we change the funtion visibility modifier, does that change the location of ...
0
votes
1
answer
70
views
Data location memory or calldata [duplicate]
Can someone tell me how to solve this problem? I am not advanced and cannot figure it out.
0
votes
1
answer
187
views
Using storage for local function variables
In one function, I was forced to declare a variable with the memory type storage (since there is mapping inside the struct). Is it much more expensive than memory? And how justified is it if I will ...
2
votes
0
answers
239
views
Converting static variable to memory
I have the following code snippet in my contract, trying to call flashLoan from Aave.
address private constant LINK = 0x...;
function myFlashLoanCall(uint256 _amount, bytes memory _params) public {...
1
vote
1
answer
47
views
Why 1225312 is the last addressable location in memory?
I was dabbling with inline assembly, and I wanted to discover the extent of addressable memory. After many trials, I found out that the only usable memory locations that could be accessed with OPCODEs ...
1
vote
0
answers
25
views
How to give an array in storage value from an array from a function's input parameters
Below is the code that I have:
struct Cow{
uint age;
uint name;
}
Cow[] cows;
// this is the code I am trying below.
function createCows(Cow[] memory _cows) public {
cows =_cows;
}
When ...
1
vote
1
answer
593
views
Data location specification and bytes32
Why is data location not required to be specified for bytes32?
1
vote
0
answers
114
views
Saving String Memory Argument to Struct Array
I have the following Solidity code that compiles and tests okay, but the insurancePlan property of the Patient struct is not being saved. I've tried many variations and it is just blank (empty) when I ...
0
votes
1
answer
2k
views
If I pass an enum as a parameter does it get stored in the blockchain?
I'm developing a Rock/Paper/Scissors Smart Contract (with a Commit-Reveal scheme).
I have this enum: enum Move {rock, paper, scissors}
I want to pass it to a function, without storing it to the ...
1
vote
0
answers
409
views
How store memory array in storage
In this code, I want to send an array of bytes32 and store them in the struct but I get the error which is
TypeError: Type string memory[8] memory is not implicitly convertible to expected type ...
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 ...
1
vote
0
answers
84
views
Private Ethereum PoA error: "out of memory"
I'm working with multi node setup on Ethereum private chain PoA. We have all three validator nodes.
blockchain
├── genesis.json
├── node1
│ ├── geth
│ ├── keystore
│ ├── ...
0
votes
1
answer
129
views
Why did I get an error about using memory keyword in function getname?
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract s12 {
uint public age;
string public name;
bool rollno;
constructor() public {
rollno= false;
}
...
0
votes
1
answer
473
views
How to returns fixed length array in solidity
I get the following error, how to resolve it?
Expected type name function Array_Data () public view returns (memory uint []) {^ ---- ^.
pragma solidity >=0.5.0 <0.6.0;
contract Lock {
uint[...
2
votes
0
answers
777
views
What is the default cache value of Geth?
According to the help page of Geth, --cache value defines
Megabytes of memory allocated to internal caching (default = 4096 mainnet full node, 128 light mode) (default: 1024)
I don't quite get it, is ...
2
votes
0
answers
424
views
How to store array parameter of a function to contract state?
I am trying to save an invoice to contract storage (see function addNewInvoice(..)) but I seem to be tripping over the data locations.
// SPDX-License-Identifier: private
pragma solidity >=0.7.0;
...
4
votes
1
answer
947
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 ...
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&...
0
votes
1
answer
36
views
The below code gets compiled but when foo is called invalid opcode error is thrown
pragma solidity >=0.5.0 <0.7.0;
contract Mycontract{
uint256 a;
struct User
{ string name; }
User[] users;
function foo() public{
User storage user=users[0];
...
0
votes
1
answer
36
views
why x(function setArr) is not being returned?
pragma solidity >=0.5.0 <0.7.0;
contract Example{
uint256[3] x;
function setArr(uint256[3] memory y) public returns(uint256[3] memory){
x=y;
return getArr(x);
}
...
0
votes
1
answer
32
views
Understanding data location
pragma solidity ^0.5.16;
contract mycontract{
uint256 public a;
function foo(string calldata _name) external {
string memory names;
names = _name;
getname(names);
...
0
votes
1
answer
647
views
Explicit type conversion not allowed from "string memory" to "uint256"
I'm using an oracle to get results from a custom api. The result is in string by default. I want to convert it to uint256, so that I can perform basic arithmetic functions.
Here's my code:
function ...
0
votes
0
answers
611
views
Memory Arrays Should Have Push() And Pop()
Dynamic memory arrays currently cannot be resized. This is true for Solang and solc solidity. However, Solang uses a heap, and has a realloc() function. Thus it is is possible for Solang to support ...
3
votes
1
answer
1k
views
Geth Version: 1.9.12-stable keeps getting killed...Out of memory: Kill process (geth) score 901 or sacrifice child
I am experiencing issues with my Geth (Version: 1.9.12-stable) nodes. Very often Geth exhausts all memory, which causes a sudden drop in blocks, resulting in the node falling behind. I am running ...
0
votes
0
answers
51
views
How to save/read mappings within struct?
I am trying to create a mapping inside struct and then create a mapping of the struct. Take a look at below example:
// ------------------------------------------------------------------------
...
1
vote
1
answer
185
views
TypeError: Member "editor" not found or not visible after argument-dependent lookup in struct PrivateSmartContract.BuildingRental storage ref
I want to store user's wallet address array as editor's address so i created struct with array like following
pragma solidity >=0.4.0 <0.7.0;
pragma experimental ABIEncoderV2;
contract ...
0
votes
1
answer
628
views
ERC20 decimals and gas costs
This question is twofold:
The standard number of decimals for an ERC20 token is 18. If we lower this number, can we expect decreased gas costs when creating/transferring the token, since this should ...
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 ...
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 ...
1
vote
1
answer
156
views
Deleting vs. Assigning to Default Values
If I have a struct with many customer accounts:
bytes32[] public customerID;
mapping(bytes32 => CustomerAccounts) public customerAccounts;
struct CustomerAccounts{
address customer;
uint ...
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
...
0
votes
1
answer
208
views
Memory Efficiency of Constant Variables
Say I have a contract with several functions that apply various particular constants.
circumference = 628318 * radius / 1e5;
dividend = balance * 123 / 1e4;
It would be easier for readers to ...
0
votes
1
answer
31
views
Modify the state using returned variable (Array.push)
In my contact I want to have one place where I find an existing object(struct) and then use it in multiple places to read and modify it - but seems like it's not possible?
contract A
{
struct ...