How does one return the first x elements of an array of addresses?

I'm pretty sure this can be done in assembly but could not find the reference.


I would lobby for a more idiomatic & cautious approach, returning one row at a time. That will give you a fixed cost per transaction at any scale. As a general rule of thumb, any process that iterates n times has a hard limit at the block gasLimit which varies over time.

address[] public myAddresses;

function getAddressCount() public view returns(uint count) {
  return myAddresses.length;

Hope it helps.

  • That makes sense. My intent is to implement a function with a fixed cost per transaction (say, returning 100 elements at a time starting from a certain index, to save calls by the front-end). My understanding is that this can be accomplished without iteration in assembly, by returning array(startPosition, length) or something like that, and it just returns that chunk in storage. Will add when I find the reference. – will_durant Dec 7 '17 at 15:58
  • Whether that's the right approach is another question. I am planning for cases where there are potentially hundreds of thousands of rows that have to be queried, which would be unwieldy to retrieve in a single call and impractical to do one at a time (although maybe that's still be the best solution, I don't know the overhead costs of making a call). – will_durant Dec 7 '17 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.