Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

Filter by
Sorted by
Tagged with
35
votes
1answer
55k views

How to initialize an empty array and push items into it?

Here's my function: function Test() constant returns (uint[]) { var myArray = uint[]; myArray.push(123); return myArray; } Here's an error I get in the solidity online compiler: ...
29
votes
3answers
15k views

Store data in mapping vs. array

I see there're some Solidity examples that are using mapping for storing state, and some are using arrays. For example this example is using mapping: struct Campaign { .... } uint numCampaigns; ...
26
votes
4answers
29k views

Solidity: Can you return dynamic arrays in a function?

I know I can return dynamic byte arrays in Solidity, such as in the following code: function createMemoryArray(uint size) returns (bytes) { // Dynamic memory arrays are created using `new`: ...
18
votes
1answer
7k views

How to clear large arrays without blowing the gas limit?

As posted by redditor ethererik in the post GovernMental's 1100 ETH jackpot payout is stuck because it uses too much gas, 1,100 ethers are stuck in limbo because the payout of this GovernMental ...
18
votes
4answers
19k views

Can solidity contract arrays receive arrays of addresses as parameters?

This question is related to this one but using address arrays instead of strings. Does solidity support arrays of addresses to be passed as method arguments? contract C { uint256 hexVal = 0xABCD; ...
17
votes
1answer
8k views

Passing an array as a parameter from javascrpt (web3) to a Solidity function

I have the following Solidity function: function func(bytes32[2] data) external { bytes32 x; bytes32 y; x = data[0]; y = data[1]; } I'm trying to call it from ...
14
votes
3answers
11k 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....
13
votes
2answers
5k views

Is it impossible to use an array of strings as the argument to solidity function?

If I try to define a function using an array of strings as an argument, solc gives me the following error: Error: Internal type is not allowed for public or external functions. function test(...
12
votes
3answers
11k 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(...
12
votes
2answers
11k views

How can I instantiate an array of addresses?

I simply want to create an array with stuff in it. Everything I try is an error message in mist. Even this code snippet I got straight from the docs wont compile: contract C { uint someVariable; ...
12
votes
1answer
5k views

Is there a maximum array size in Solidity?

And if so, what is it? Also, would it differ by dynamic and static arrays? My initial guess is that: a statically-declared array has a maximum number of slots equal to the largest uint, as in array[...
11
votes
1answer
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++) ...
9
votes
1answer
2k views

TypeError: different number of components either side of equation

I am working on a project using ERC721. Code below pragma solidity >=0.4.21 <0.7.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract Sketch is ERC721 { string[] ...
9
votes
2answers
7k views

How to concatenate a bytes32[] array to a string?

I am passing an array of bytes32 to my function. This array can contain numbers or hashes etc. to identify an object. In a new use case I want to pass a URL to this function by splitting the URL into ...
9
votes
3answers
2k views

How can I create complex array relationships in solidity?

Hello I'm trying to work out how to create complex relationships in Solidity. In this example a customer will place an order using a function with arguments, however it cannot be for any combination ...
9
votes
1answer
3k views

How to manually initialise an array in Solidity?

I can't find a way to manually initialize an array. string[] public cool; function constructor() { cool[0] = "one"; } Won't execute, while function constructor(){ cool[cool.length++] = "...
9
votes
1answer
678 views

Is it convenient to use mappings as temporary KeyValue instances?

Consider the following contract that illustrates the problem. Here we have a dynamic array of mappings, add() is meant to add a fresh new mapping to the end of the array, return the value for mapping ...
8
votes
3answers
14k views

Solidity initialize memory array of fixed size

Using solidity 0.4.15, there is a function that takes a number and returns array of fixed length called traits: function splitN(uint256 n) constant returns (uint8[12]) { uint8[12] memory traits = ...
8
votes
2answers
10k views

Calling public array of structs using web3

Good day. I've got an array of structs such as: User[] public users; struct User{ uint idNum; string name; address userAddress; } And I'm trying to get the array of users with web3.js ...
8
votes
1answer
2k views

Inline initialization of integer array

These two lines compile: uint8[5] foo = [0,0,0,0,0]; int[5] foo = [1,0,0,0,0]; My question is, why do the following two lines not compile and how can I fix them? int[5] foo = [1,-1,0,0,0]; Error:...
8
votes
0answers
3k views

"Push" is not available in bytes32[], memory out of storage [duplicate]

Here is the function of my code where error comes ``` function getPeople() constant returns(bytes32[], bytes32[], uint[] ) { uint length = people.length; bytes32[] memory firstNames = new bytes32[]...
7
votes
1answer
5k views

Initialize struct with an empty array

I am trying to initialize this struct and I can't figure out how to pass in a dynamically sized empty array.. uint8[] id; users[msg.sender] = User( true, msg.sender, _username, block....
7
votes
3answers
3k views

Function with more than 16 arguments

At some point in my contract I have a public, not Constant function that has more than 16 arguments (24 to be exact). I'm getting an error saying Stack too deep. Try removing local variables I can't ...
7
votes
1answer
5k views

How to initialize empty struct array in new struct?

Trying to do this: contract Foo { mapping (bytes32 => Thing) things; struct Thing { Item[] items; } struct Item { uint number; } function Foo(bytes32 id) ...
7
votes
2answers
4k views

Solidity: Initialize struct containing string array

I do not seem to be able to initialize a struct containing a string array. This is what I tried: contract testStruct { struct stru{ string[] s; } stru myStru; /* ...
7
votes
1answer
462 views

What's the limitation that requires Solidity array dimensions to be declared backwards?

From docs: In Solidity, array dimensions are declared backwards from the way you might be used to declaring them in C or Java, but they are access as in C or Java. For example, int8[][5] ...
7
votes
1answer
15k views

How to loop through a array of addresses?

I have a mapping and an array that contains a list of addresses of students. When I loop over that array, the method .length is not working. How can I loop through the array Student_list shown below?...
7
votes
4answers
492 views

What are the benefits of deleting storage array elements?

I have been wondering as to what benefits we get from deleting elements from a storage array in ethereum contracts ? That particular state is already in a block isn't it ? Could someone please ...
7
votes
4answers
916 views

Splice Struct Entry by Index in Solidity

I have a struct and I'd like to remove an item by index: struct Payout { address addr; uint yield; } Payout[] public payouts; I'm not familiar with structs and more use to dealing with arrays ...
7
votes
1answer
2k views

Compare structs and arrays with keccak256 in order to save gas

Just working on a solidity code last day, I figured out that apart of comparing bytes, strings etc... The Keccak256 will be the perfect solution for comparing large arrays or big structs. Because as ...
7
votes
1answer
2k views

What is the array size limit of a returned array from a contract function call in Solidity?

I have a contract storing rdf data in various mappings. It is all working lovely except for large array returns. I have functions to return arrays of hashes matching certain criteria. I have found, ...
7
votes
2answers
1k views

Problem accessing array inside struct

Dealing with the code below: contract myContract { struct myStruct{ bytes32 name; bool active; uint[] changes; } myStruct[2] myStr; uint[] steps; Event ...
6
votes
2answers
3k 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 ...
6
votes
3answers
877 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 ...
6
votes
1answer
3k views

TypeError: Indexed expression has to be a type, mapping or array (is function (address) view external returns (string memory,string memory...)

I have 2 contracts first one is sub second one is main . First i deploy main and create a new subtoken. Then I want to access my maincontract in subToken contract but I cant :( When I compile this ...
6
votes
1answer
434 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
2answers
943 views

Accessing to a dynamically sized array value in a contract

I'm trying I have this contract: pragma solidity ^0.4.2; contract ArrayContract { address _owner; uint256[] array; function ArrayContract (){ _owner = msg.sender; array....
6
votes
1answer
1k views

2 mapped structs with an address array - push not working

Can someone help me understand why the function add in the contract one is not working and in the contract two is? Both are mined and are inserted into the blockchain. Thanks. Not working contract ...
6
votes
1answer
8k views

Unable to add new struct item into mapping or array

trying to do a sample based on struct, storing a list of struct as a mapping. but when i add an item and trying to retrieve i cannot find that item in the mapping below is my contract contract ...
5
votes
1answer
3k views

Solidity 0.5.x - make address array payable

I'm trying to write a contract that uses an address[] array which should contain payable addresses. But apparently creating a payable address array doesn't work in solidity 0.5.x. All I'm trying to ...
5
votes
1answer
4k views

How can I slice bytes, strings, and arrays in Solidity?

Is it possible to slice variables in Solidity?
5
votes
1answer
3k views

Array or mapping, which costs more gas?

I have been using arrays of struct for my contract. In Remix IDE with the private network, one function costs me around 300,000 gas. However, if I call the same function from web3js, it costs me a lot ...
5
votes
2answers
1k 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 ...
5
votes
2answers
588 views

How do you implement PubSub without running out of gas (attack vector)?

A simple PubSub pattern involves an array of subscribers. Anyone can subscribe. A publish function will do something for each subscriber. Can you spot the attack vector...? contract PubSub { ...
5
votes
2answers
770 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
1answer
6k views

Constructing contract with an address array

Please correct me if I am wrong but I understand you cannot initialise a contract with a string array as it is a dynamic type but can I initalise a contract with a fixed type? specifically an array of ...
5
votes
2answers
1k views

Standard work-around for using a Solidity constant array (which is not supported) in pure function?

I have an array in my contract like this and a pure method which uses it like this (simplified exmaple because my real exmaple is many more lines): uint[16] constant x = [10, 2, 6, 42, 19, 5, 4, 36, ...
5
votes
1answer
5k views

Solidity function that accepts mapping as input

Is it possible for Solidity to accept mappings as input parameter in a function? I've been trying to create a function with a second parameter mapping (string => string) aMapping, pragma solidity ^0....
5
votes
2answers
580 views

Return the struct inside array inside array

pragma solidity >= 0.4.25 < 0.6; contract Bank { struct Customer { uint age; } Customer[][] customers; function foo() public { if (customers.length > 0) { ...
5
votes
1answer
2k views

Delete or clear a mapping

From what I am gathering based on this question and this question it seems that I am better off not trying to clear arrays or mappings. As the documentation on mappings indicates every possible key is ...

1
2 3 4 5
10