Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

Filter by
Sorted by
Tagged with
31
votes
1answer
39k 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: ...
23
votes
3answers
11k 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; ...
22
votes
4answers
20k 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
6k 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 ...
17
votes
1answer
6k 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
4answers
11k 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; ...
13
votes
2answers
4k 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(...
11
votes
2answers
7k 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; ...
11
votes
1answer
3k 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
2k 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
2answers
6k 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
579 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
2answers
7k 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(...
7
votes
1answer
4k 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
2answers
4k 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....
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
440 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
2answers
9k 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 ...
7
votes
4answers
440 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
607 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
817 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:...
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 ...
7
votes
0answers
2k 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[]...
6
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) ...
6
votes
3answers
2k 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 ...
6
votes
1answer
968 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
931 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 ...
6
votes
1answer
341 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 ...
5
votes
2answers
7k 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 = ...
5
votes
2answers
523 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
621 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
3answers
306 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 ...
5
votes
1answer
642 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....
5
votes
2answers
261 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
951 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, ...
5
votes
1answer
370 views

How initialize with empty list?

I don't how initalize a variable with empty array ? I have two structures : struct Concour { string theme; address admin; uint dateEndInscription; uint dateEndBet; uint ...
5
votes
1answer
6k 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
2answers
734 views

Optimizing storage for an array of addresses

When possible, does Solidity optimize a storage array of address? For example, does address[8] take up 5 storage slots (160 bytes), or 8 storage slots? contract C { address[8] arr; } If 8 slots ...
4
votes
2answers
367 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 ...
4
votes
1answer
872 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 ...
4
votes
2answers
2k 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 ...
4
votes
1answer
4k views

Store addresses in array or mapping?

My contract currently serves an array of addresses to a front-end: address[] public addresses; function getAddresses() public view returns (address[]) { return addresses; } I'm concerned about ...
4
votes
1answer
5k 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 ...
4
votes
1answer
2k views

Accessing mapping inside object with web3.js

Good day. I have a struct object called HomeWork HomeWork[] public homeworks; struct HomeWork{ uint256 assignId; string description; mapping(address=>uint256)handedAt; mapping(...
4
votes
1answer
177 views

Why does the wallet contract convert addresses to integers?

In the multiowned part of the wallet contract, owners are stored in a uint array, such as in line 59 in the constructor. Here is a relevant excerpt: contract multiowned { // METHODS function ...
4
votes
1answer
1k 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 ...
4
votes
1answer
328 views

Search in Address[] if exist the same address sender

I want to check if the Address sender is already in Address[]. If true, then do not allow this address to make the payment. Contract Hello { address[] internal Pool; function () public payable { ...
4
votes
1answer
2k views

Returning dyanamic array from function

I need to access books array of Student contract in ClassRoom contract. If i write as function readStudentStruct (uint ID) constant returns (address, uint, bool,address[]) then error Different ...

1
2 3 4 5
7