• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How does the post increment work really?

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a code issue and I have a below question for the same.

Assuming data[] is of type byte[], is the below statement in java -



same as -



?

I tried with


That also doesn't seem to be the same thing. I am not putting code around this but essentially if I make this change for not using data[p++] code stops working! (This is the only change that is breaking the code).

Thanks
Maulin
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maulin Vasavada wrote:Assuming data[] is of type byte[], is the below statement in java - [...] - same as - [...]?


No; in the first case p will be 1 greater than the point at which the mismatch was found after you exit the loop; and I would expect BOTH to fail with an ArrayIndexOutOfBoundsException unless you add a check for length.

Question: Why not just use:or indeed:Both seem rather clearer than what you've got. And the problem isn't really anything to do with post-increment; it's to do with the fact that you're cramming an awful lot of logic into a single statement when you don't need to.

Write Dumb Code.

Winston

PS: You do realise that '(data[p] & 0x80) == 0x80' is just a long-winded way of saying 'data[p] < 0', don't you?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston

I completely agree to your comments about why we have to re-write the same logic in better way.

I am refactoring the code and I ran into this. I will try options you suggested and post the findings.

Thanks
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I fixed the code as below and now it works,



Thanks
Maulin
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us what the condition in () after while means. You may wish to crib the answer from Winston't post
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maulin Vasavada wrote:Okay, I fixed the code as below and now it works...


Hmmm. Seems an odd way to go. I'm particularly interested in the "Step forward by one more" increment, because that will put you at the index after the mismatch. So if, for example, you found a mismatch at index 3, that will set it to 4, which seems like a strange requirement.

However, if it works, don't fix it.

Winston

PS: I still don't see any check for length.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston

We do have the length check somewhere before this code and this code is a specific parsing logic for a specific data standard we have. For the purpose of this thread we can just focus on 'technical' aspect of why earlier it didn't work for me (after my code change) is ,

1. The post increment in the original code was happening even when while condition didn't match. So to you point it IS going one pointer ahead than the actual mismatch point ('why' part of this we will blame on the 'specific' parsing logic )
2. To fix that I added one more p++ after the while loop after my change


Thanks
Maulin
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maulin Vasavada wrote:1. The post increment in the original code was happening even when while condition didn't match. So to you point it IS going one pointer ahead than the actual mismatch point ('why' part of this we will blame on the 'specific' parsing logic )
2. To fix that I added one more p++ after the while loop after my change...


In which case, I suspect it's wrong, and you just haven't discovered when yet. I could be wrong, but those first loops you listed suggest not.

The loop will only exit when there is a MISmatch, at which point p will hold the index at which it occurred. It seems a very strange thing to then increment the index again (without a length check) to get the index after that.

However, if that's the way it works and you can understand it, don't let me stop you.

Winston
 
Hot dog! An advertiser loves us THIS much:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic