Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

Filter by
Sorted by
Tagged with
0
votes
1answer
282 views

Solidity: Filling a storage array

What is the easiest way to fill a storage array? uint[4] temp; temp = [1,2,3,4]; >> TypeError: Type uint8[4] memory is not implicitly convertible to expected type uint256[4] storage pointer ...
3
votes
2answers
599 views

Returning 2D array with dynamic sizes from a function

This 2D array works when setting the array lenghts with constant values. But i'd prefer to use a dynamic array size: function funWith2DArray(address[] array1, address[] array2) public view returns (...
0
votes
2answers
306 views

Push to Array does not actually push

Community, I've been banging my head over the following problem and hope you can spot my mistake. I deploy the following contract with Populus on Testrpc. This is pretty simple contract: pragma ...
2
votes
1answer
3k views

Pushing a struct to an array which contains an array itself

I was reading this article, which gives an example of a contract which won't work. Though they have fixed it in the article, I wanted to know why it doesn't work. I have copied a simplified version ...
2
votes
1answer
930 views

What is the right way to create a struct instance inside a function?

I read somewhere that if you create a storage variable inside a function, it could overwrite another existing one in the global scope of the smart contract, is that true? So, if I wanted to create a ...
0
votes
1answer
1k views

Working with structs in Solidity and web3js [duplicate]

I'm trying to interact with this contract via web3.js. Something like this answer seems to be the right path to take, but I can't figure out how to implement that logic in my contract. When ...
2
votes
2answers
417 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
316 views

Why would you use an array as an input?

I have the following contracts: pragma solidity ^0.4.17; contract one{ uint firstNum; uint secondNum; uint thirdNum; function getNumber(uint _num, uint _num2,uint _num3) public{ ...
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 { ...
1
vote
1answer
358 views

double mapping to an array only allows one item?

I have the following: mapping (address => mapping (address => uint[])) public allowed; function approve (address _to, uint256 tokenId) external { allowed [msg.sender][_to].push(tokenId); } ...
0
votes
1answer
617 views

returning array of struct in remix

Seems like there are many related questions ex: here, but I still can't believe it is not supported in remix or am I misunderstanding it? seems like it is supported in production. struct Test { ...
0
votes
1answer
689 views

Can't get length of array? (not querying for property)

mapping(uint256 => address) public userList; function getUserCount() public constant returns(uint userCount) { return userList.length; } function newUser(address _address) internal returns(bool ...
0
votes
1answer
864 views

returning an uint array + gas + cost efficient problem?

I have a growing public array struct MatchInfo { uint256 matchId; bool isMatchDone; } MatchInfo[] public matchInfo; and I want to return an array of uint256 which will loop through ...
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 ...
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 ...
1
vote
1answer
81 views

Fixed-size array issue

Maybe this question it seems to be stupid but is it possible to change elements of fixed-size array like bytes32. For example: bytes32 _b32 = sha3(1); _b32[0] = 100; // => Error
0
votes
1answer
390 views

Best way to handle unique ids

A msg.sender can create multiple Struct Item . What would be best way to store the Item ? Currently I'm using struct Item { uint256 amt; bool isActive; address owner; } Item[] public ...
0
votes
1answer
256 views

Struct Array length is always 1

This is the contract im calling createP to push Post struct Post { uint256 amt; bool isActive; address owner; } Post[] public posts; function createP(uint256 ...
1
vote
2answers
404 views

Returning static array inaccessible dynamic type

In the first contract I have: function getArray() external view returns(uint[] memory result) { uint counter = 0; result = new uint[](3); for (uint i = 0; i <= 3; i++) { result[...
0
votes
1answer
527 views

Push to an array created inside a function

I want to create a temporary array that is returned from a function. I declare it in the function and use the memory tag but I am unable to push to it. function getAllLandForSale() external returns(...
1
vote
1answer
1k views

Can I modify arrays created in mappings in Solidity?

I have created a mapping which gives an array of structs. mapping(address => BetByUser[]) betsByAddress; I have tried changing this array, but with no success. Are these arrays in memory or ...
3
votes
3answers
3k 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 ...
1
vote
0answers
57 views

What's the suggested way to create a 2D array which can be sparse at the beginning and grow dense later?

I can think of two ways: 1) Allocate a 2D array: uint256 public W = 15; uint256 public H = 15; uint256[W][H] public grid; 2) Use mapping: mapping (uint8) => mapping(uint8 => uint256)) public ...
1
vote
1answer
570 views

Get the minimum value in array solidity

I have an array of uint in Solidity, How can I find the minimum element in the array using web3js? One option is to create a getter to the array and fetch all the values, but isn't that too long ...
-1
votes
1answer
97 views

Trying To Retrieve An Array Element Returns Null

This is my contract: contract MessageContract { string[] message; function setMessage(string _message) public { message.push(_message); } function getMessage(uint i) public ...
0
votes
1answer
57 views

Unable to change the length of a global 2D storage array

I am facing difficulties changing the length of a global 2D storage array. It seems to be working correctly on Remix IDE, but when I try to test it on ganache-cli (formerly known as testrpc) - either ...
2
votes
2answers
291 views

Returning bytes from array of structs

In solidity do i return an array or structs in bytes. This is not a duplicate of this question because that solution always returns the error: new BigNumber() not a number: [object Object] pragma ...
1
vote
1answer
34 views

What is the length of an empty fixed sized array?

Let's say we initialize a state variable as: uint[10] public fixed_array; This is a fixed sized array which is currently empty. What would fixed_array.length return? 0 or 10?
0
votes
1answer
1k views

string memory[3] memory is not implicitly convertible to expected type bytes32[] storage ref

I have the following (array of bytes32 types): bytes32[] public allTokenSymbols = ["0x4554480000000000000000000000000000000000000000000000000000000000", "...
0
votes
2answers
895 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 { // ...
2
votes
1answer
495 views

Iniitalize struct in struct

How I do I push the first value to this: struct Balance { address owner; uint amount; DeepBalance[] deepBalance; } struct DeepBalance{ address swap ; uint amount ; } ...
0
votes
2answers
714 views

Avoiding duplication in array

Here is my code : pragma solidity ^0.4.14; contract AdminSetUp { // contract for adding new admin address private creator = msg.sender; address[] public adminlist; uint public count; modifier ...
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 ...
0
votes
1answer
539 views

Solidity arrays not storing values

I have written a basic contract like this to store certificates: pragma solidity ^0.4.2; /// @title Sending and storing certifications to the blockchain contract CertificationStore { ...
0
votes
1answer
107 views

Writing to contract struct via function parameter, memory vs storage, “memory outside of storage”

This is my first contract ever (building on an own example learning case I made up). Given this Contract (simplified): unit gEval = 0; contract Group { struct Grp { string name; uint ...
1
vote
2answers
344 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
1answer
2k views

Hidden problems in “set array to empty array to clear it” approach

I needed a way to clear a storage array in my contract. I've seen this answer, but I tried to think of different approach (My arrays are most probably will contain enormous amount of elements) and ...
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(...
1
vote
1answer
46 views

TypeError in Array Traversal

solidity 0.4.17 I have an array of int128 elements, I chose in128 because I want to check for -1. int128[] elements; int128 numberOfElements;//I increment this as I add elements function hasElement(...
1
vote
1answer
851 views

Is there a maximum size for arrays in Solidity?

In my solidity code I define 3 arrays with around 700 elements inside. However, when I try to deploy the contract using Geth I am having the following error message: Error: oversized data undefined ...
2
votes
1answer
1k views

VM error: invalid opcode resulting from array operations

pragma solidity ^0.4.18; contract VectorTest { /* this runs when the contract is executed */ function VectorTest() public { // initialize the variables } // returns an int ...
0
votes
1answer
252 views

Adding address to Struct's array of addresses does not persist

I'm having trouble getting the array of "watchers" to persist after I: Deploy the contract Create Magic with name "test1" Execute watchMagic (with test1's magic id) Execute watchMagic (with test1's ...
1
vote
2answers
1k views

Deleting an array element. Need to resize to return correct length? [duplicate]

I want to get rid of the last item of a storage array, and have array.length return the proper value. Currently I'm using delete to zero it out. However, when I then call array.length, it returns the ...
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 ...
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 ...
3
votes
2answers
1k views

Can I filter through an array of structs?

I want a way to store an array of structs. And then I want the ability to filter through this array and pick out a subset of items where a certain struct parameter named foo == bar for example. Is ...
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 = ...
4
votes
1answer
808 views

Assigning an array creates a pointer or copies an array? (Solidity)

Here is the code: pragma solidity^0.4.16; contract Test { struct SomeData { address[] users; } address[] nullArray; mapping(uint => SomeData) SomeDataList; function ...
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
1answer
68 views

How do I implement a deque in Solidity?

I have an array of finite size, say something like uint[100] Arr;. My question is, if I want to continually push to this array, what happens if it already has 100 uints in it and I push another one? ...

1
3 4
5
6 7