Skip to main content
Share Your Experience: Take the 2024 Developer Survey
179 votes
Accepted

When should I use calldata and when should I use memory?

memory and calldata (as well as storage) are keywords that define the data area where a variable is stored. To answer your question directly, memory should be used when declaring variables (both ...
Shane Fontaine's user avatar
54 votes
Accepted

How to store IPFS hash using bytes32?

Your example shows storing an IPFS identity using it's alphanumeric encoding (Qm...), which is the same Base58 encoding that Bitcoin uses. However, what it's representing at its core is a number (the ...
MidnightLightning's user avatar
40 votes
Accepted

Can I save structs in a Mapping?

You can store structs as the values of your mapping, not as the key. Here is some more info: http://solidity.readthedocs.org/en/latest/types.html#structs struct Funder { address addr; uint amount;...
samurai jack's user avatar
  • 1,125
39 votes
Accepted

How to fill dynamic in-memory array

Dynamic arrays are only available in storage, not in memory. In your case, the size of the result array is known upfront (n). So, you can just declare an array with length of n. Then you can fill it ...
Henk's user avatar
  • 4,271
28 votes
Accepted

TypeError: Data location must be "storage" or "memory" for parameter in function, but none was given

As of Solidity 0.5.0. https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also ...
Rob Hitchens's user avatar
  • 55.3k
24 votes
Accepted

How to initialize an empty array inside a struct?

There is no need to initialize storage arrays in Solidity. Only memory arrays has to be initialized before usage. So in your case, no need to initialize x inside Bar as long as you are not assigning ...
Muhammad Altabba's user avatar
14 votes

Invalid array length, expected integer literal

Generally for an array, you would use uint[] memory list = new uint[](some_size);
Mr. T's user avatar
  • 345
14 votes

When should I use calldata and when should I use memory?

memory variables are allocated by the callee and their value can be modified inside the function (they're mutable). You can declare a variable inside a function memory located as well as it's ...
R01010010's user avatar
  • 281
13 votes
Accepted

How to pop from (decrease the length of) a memory array in Solidity

Here's a way to do backerList.length--; on an address[] memory backerList by using inline assembly: assembly { mstore(backerList, sub(mload(backerList), 1)) } Some important points to remember: ...
Jesbus's user avatar
  • 10.5k
11 votes

Can I save structs in a Mapping?

Yes, you can. For example, in the solidity code below. There is a struct User that has a mapping of friends, from address to another struct Friend. Then there's a mapping of users, from address to ...
Daniel Okwufulueze's user avatar
11 votes
Accepted

error: Storage location can only be given for array or struct types

The term "storage location" is a bit misleading here, it refers to a more general concept about where data is stored, i.e. in storage or in memory. The error occurs because the "storage location" is ...
chriseth's user avatar
  • 3,412
11 votes
Accepted

How to run geth at 512Mb RAM Digital Ocean droplet?

It seems like 2 Gigs of swap and swappiness 30 is enough to run geth with no crashes with just 512 Mb of RAM. It is almost 2 months now. I'm in full sync (block 3097083). And had no crashes ever. So ...
takeshi's user avatar
  • 1,760
11 votes
Accepted

The difference between contract address and wallet address

An account address - either an externally owned account (what you're calling a wallet), or a contract account - isn't the same thing as a memory address. It's not possible to know what exists at "...
Richard Horrocks's user avatar
11 votes
Accepted

How much data can I store in a smart contract, what is the cost and how it is implemented?

Gas is the only cost you have. There is no explicit "keep in storage" cost as such so if you pay X amount of gas when you save something in a smart contract then you never have to pay for that storage ...
Lauri Peltonen's user avatar
9 votes
Accepted

Why does PUSH1 0x60 PUSH1 x40 MSTORE store 0x60 at memory location 0x50 (and not 0x40)?

Each word in the EVM is 32 = 0x20 bytes wide, so when you use PUSH1 40 to store a word at 0x40 it fills up all the bytes up to 0x5f. The EVM uses "big-endian" format, so the least significant bytes of ...
benjaminion's user avatar
  • 9,297
8 votes
Accepted

Why memory arrays are unresizable?

tl;dr You are right that it is not possible currently, but there's no fundamental reason why Solidity couldn't implement the feature in future. Fully dynamically sized arrays don't sit easily with ...
benjaminion's user avatar
  • 9,297
8 votes

How to store IPFS hash using bytes32?

Here are some js functions for stripping and re-adding the first two bytes containing the hash function and size, suitable for web3. import bs58 from 'bs58' // Return bytes32 hex string from base58 ...
Stan James's user avatar
8 votes

How much data can I store in a smart contract, what is the cost and how it is implemented?

Just to add to Lauri's answer. the memory is represented as an array of 2 ^ 256 32 bytes slots, but can I really use all this memory? If you pay for it, yes. Note that this equates to 3.705 x 1066 ...
Richard Horrocks's user avatar
8 votes
Accepted

How does this assembly code create a new "memory end"?

It explained a little later in the same documentation: Solidity manages memory in a very simple way: There is a “free memory pointer” at position 0x40 in memory. If you want to allocate memory, ...
Ismael's user avatar
  • 30.5k
8 votes
Accepted

Efficient Bit Packing

In Solidity a bool is stored in 1 byte (8 bits). However, only a single bit is actually used. Under the hood, The hex value of true is 0x01, false is 0x00. Converted to binary these are 00000001 and ...
iamdefinitelyahuman's user avatar
7 votes
Accepted

Is it possible to decrease RAM usage of go-ethereum(geth)?

It is not possible to reduce RAM usage of geth or any other application for that matter. Simply starting a geth node will not consume much of your RAM, but when you start running commands to create ...
galahad's user avatar
  • 3,920
7 votes

The difference between contract address and wallet address

In Javascript, EOA(wallet address): '0x' + keccak256(publicKey).slice(-40) Like above, wallet address can be achieved by public key. 1) hash it with keccak256. 2) slice rightmost 20 bytes(40 words). ...
nujabes's user avatar
  • 191
6 votes
Accepted

What is the composition of the blockchain database?

account is a local object to easily track changes on the accounts (with a StateObject db). The size depends on the nbr of transactions you execute with this account. An unused account should be less ...
wiwulo's user avatar
  • 131
6 votes
Accepted

Invalid array length, expected integer literal

As per documentation: Creating arrays with variable length in memory can be done using the new keyword. You can use following code snippet to solve your problem. uint length =10; bytes memory ...
Prashant Prabhakar Singh's user avatar
6 votes

How to run geth at 512Mb RAM Digital Ocean droplet?

With that limited of resources your best bet is to run in light client mode. Yes it is in alpha but is very usable as long as your just playing around with it, and not using large sums. Depending on ...
John Gerryts's user avatar
6 votes
Accepted

How does dynamic array memory reallocating work?

Solidity never reallocates storage arrays (memory arrays can't even be resized). Solidity relies on the fact that all storage array locations are based on a KECCAK256 hash, and that KECCAK256 is ...
James_pic's user avatar
  • 1,110
6 votes
Accepted

memory keyword in function argument

If you don't specify storage type of a function argument then function arguments are always in memory and you are passing an argument to a function by value. When you are explicitly defining storage ...
Soham Lawar's user avatar
  • 2,567
5 votes

How to run geth at 512Mb RAM Digital Ocean droplet?

Running it on such a low amount of RAM might be tricky. I know the devs have made attempts to reduce RAM usage so it could run on Raspberry PI, but even that has 1GB of RAM. In addition to making a ...
5 votes
Accepted

Is the EVM memory model a word array or byte array?

I view it as a byte-addressed byte-array. It is certainly byte-addressed, in that consecutive integer addresses refer to consecutive bytes in memory. It is true that you can only read a 32 byte word ...
benjaminion's user avatar
  • 9,297
5 votes

Type error: memory is not implicitly convertible to expected type

The compiler thinks you are trying to store the return value of getBetsForMatchup in contract (permanent) storage. Try changing BetProposition[] betsToCancel = getBetsForMatchup(matchupIdentifier)...
Howard's user avatar
  • 421

Only top scored, non community-wiki answers of a minimum length are eligible