Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

Filter by
Sorted by
Tagged with
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 ...
22
votes
4answers
21k 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`: ...
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; ...
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(...
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 ...
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 ...
6
votes
1answer
986 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 ...
31
votes
1answer
41k 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: ...
6
votes
1answer
7k 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 ...
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 ...
0
votes
2answers
3k views

How to output all data from contract's mapping variable

Solidity enables the use of associative arrays with mapping: mapping(address => uint256) balances; Code can be written to iterate through this associative array. But, how can all of the keys and ...
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 ...
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 ...
3
votes
1answer
295 views

Geth - reading public address[] from contract

After setting up testrpc and attaching geth: testrpc -a 10 geth attach rpc:http://localhost:8545 I initialise the following contract on the geth command line: pragma solidity ^0.4.6; contract ...
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++] = "...
0
votes
1answer
99 views

2d array of different types?

I got quite an interesting question. I have created an array of structs, and in each struct, I have an array of address. Then I got a method that when its called on given struct [index] its adds ...
0
votes
1answer
311 views

How to define a dynamic struct “field”?

We have a struct as follows : struct ObjectStruct { bytes32 state; address owner; bool isObject; } mapping(bytes32 => ObjectStruct) public objectStructs; bytes32[] ...
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(...
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[...
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 ...
3
votes
3answers
4k views

Efficient approach to delete element from array in Solidity

For each user, I want to keep an array of holded assets (each asset has an ID). My solution up until now is : struct User { uint userId; uint[] assets; } For every asset the ...
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....
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) ...
2
votes
1answer
2k views

Is is possible to insert objects into an Array at the specified position and shift indexes?

I have following code piece: struct PaymentReceipt{ uint start_block; uint end_block; } PaymentReceipt[] paymentReceiptList; function verfiyPayment(uint block_start, uint ...
2
votes
1answer
3k views

Error encountered during contract execution [Bad jump destination]

When calling to start() function in a Smart Contract, transaction fails with this message: Warning! Error encountered during contract execution [Bad jump destination] According to Error During ...
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++) ...
11
votes
2answers
8k 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; ...
4
votes
1answer
6k views

How to convert a string to bytes32 array?

I have known how to convert a string to bytes32. But if the string length is longer than 32, the rest of the string will be truncated. So how to convert a string to bytes32[] array?
4
votes
1answer
3k 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(...
3
votes
2answers
2k views

Initialize a fixed size, 2 dimensional array within function

In Solidity, how do I define a fixed size, 2 dimensional uint8 array scoped to a function, initialized with all zeros? E.g. func(){ var myArr = new uint8[5][5](); // which does not work. }
2
votes
2answers
430 views

Resetting storage slot increases gas usage although should decrease it

I have a simple contract that deletes the last element of the array: pragma solidity^0.4.11; contract GasRefundTest { uint[] myArray = [1, 2]; function deleteLastElem() public returns(...
2
votes
1answer
996 views

Why is there a difference between a public array and a function which returns it?

On-chain example: pragma solidity 0.4.24; contract MyContract { uint[] public array; constructor() public { array.push(1); array.push(2); array.push(3); } ...
1
vote
2answers
204 views

Parsing contract method output as an array of fields

I have a constant method in a Smart Contract that returns this array: (uint256 _field1, uint256 _field2, uint256 _field3, address _field4, string _field5) When invoking it I got this hex result: ...
0
votes
1answer
157 views

How can I push data to array when using mapping (address => address[]) mappingName?

What is the exact syntax to push data using the above given syntax?
0
votes
2answers
2k views

An error when trying to return a static array

This compiles: This don't: Untitled:3:9: Error: Type uint8[2] memory is not implicitly convertible to expected type uint8[2] storage pointer. uint8[2] z = [255,255]; ^----------------...
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 ...
3
votes
1answer
194 views

Geth - reading addresses inside mappings from a contract

This answer shows that we can return all the addresses (from address[] public) as a single array of addresses: function getAddresses() constant returns (address[]){ return MemberAddresses; ...
3
votes
1answer
3k views

How to return bytes array in Solidity Contract

I'd like to input & output an array of bytes (of approximately 56 characters each). The input works but the compiler gives a TypeError on bytes[] for the output function: TypeError: Internal type ...
2
votes
2answers
3k views

Error Encoding Argument (remix)

We have a contract as follows: pragma solidity 0.4.23; contract RFID { struct StateStruct { bytes32 description; mapping(bytes32 => bytes32) sub_state; ...
2
votes
1answer
1k views

How to delete element of array, without index?

When i add element into array, and after adding, i don't know index of this element. How to delete element in this case? I can find index, using while, but i think it's very bad thing, because its ...
2
votes
1answer
2k views

Passing a struct to a function gives TypeError: Internal or recursive type not allowed

I am trying to pass in a struct to the constructor of a contract but am getting the error TypeError: Internal or recursive type is not allowed for public or external functions The contract is ...
2
votes
1answer
579 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 ...
1
vote
2answers
362 views

Invalid Op Code - passing bytes32 array to a contract method called from another method of another contract

I've been banging my head on this one for some hours, I'd appreciate any help :) This is a final edit to clearly state the issue: Is there a proper way to pass a bytes32 array to a method on contract ...
1
vote
0answers
317 views

Mocha timeout, not resolving promise of large array from method.mymethod.call()

I'm trying to test a contract getter method that returns a large array. I am testing using the web3 library in Mocha with ganache-cli provider. My contract deploys alright, but trying to call the ...
1
vote
1answer
97 views

Smart contract returning values as zeros

pragma solidity ^0.4.18; contract TransManager { //state variables // contract owner address private creator; uint transCounter; // File Meta metaData struct metaData{ uint id; ...
1
vote
0answers
92 views

Remix testing timeout with JavaScript VM when calling a large array

I'm trying to test a contract getter method that returns a large array. I am using Remix and deploying using JavaScript VM. My contract deploys alright, but trying to call the getArray() method causes ...
1
vote
1answer
563 views

Code Explanation of how to return an array of structs

I was reading a tutorial on a medium about how to return an array of structs. This is the code they have for doing it. pragma solidity ^0.4.13; contract Project { struct Person { ...
1
vote
1answer
688 views

Accessing External Contract's Struct Array mappings

I have struct in an external contract that I need access to Contract Ext{ Struct user{ uint dailybalance; } mapping ( address => user[] ) userDailyBalances; } How do I access the ...
0
votes
2answers
935 views

Increase all values of mapping or array without loop

Would it be possible to increase all the values of a mapping or array (without a loop)? Example: mapping(address => uint) score; uint[] values; function increase_scores() public { // ...
0
votes
1answer
28 views

Read List of Posts using struct

I tried to read a list of posts using the code below struct posts { bytes32 postTag; string post; address[] acceptedBy; uint8[] voteCount; } mapping(address => posts) totaluserPosts; ...