0
address[] twitteruser;

function twitterhandles(address _address,string _twitterhandle)  public  {

    for(uint i=0;i<=twitteruser.length;i++){

        if(twitteruser[i] ==_address)
        throw;
    }

    handles[_address].twitterhandle = _twitterhandle;
    twitteruser.push(_address);
}

Why is the output is getting thrown? Any help is appreciated. Thanks in advance.

  • Hi there. Are you saying that the condition in the if statement is being met and throw is being called, and you want to know why? Or something else? – Richard Horrocks Oct 11 '18 at 20:08
  • yes. This is the case .This is just a sample code where I want to iterate through the dynamic array. – Kartikey Oct 12 '18 at 5:00
1

As this is coded there are at least two obvious reasons why it might throw.

Setting aside a malformed transaction (possible), the other two are:

  1. The twitter user already exists which is not allowed, because if(twitteruser[i] ==_address) throw;
  2. There are too many twitter users because cost increases with each new twitter user. It will get more expensive to add them. Eventually, it will be impossible to add them at any cost.

for(i=0; i < unlimited; i++) is a well-known anti-pattern.

Hope it helps.

| improve this answer | |
  • this is just a sample code. here I just want to iterate through the dynamic array. but it is giving a throw in its first insertion. – Kartikey Oct 12 '18 at 5:03

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.