Click here to Skip to main content
15,892,005 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Just because you can doesn't mean you should. Pin
honey the codewitch14-Dec-19 16:44
mvahoney the codewitch14-Dec-19 16:44 
GeneralRe: Just because you can doesn't mean you should. Pin
PIEBALDconsult14-Dec-19 18:47
mvePIEBALDconsult14-Dec-19 18:47 
GeneralRe: Just because you can doesn't mean you should. Pin
honey the codewitch14-Dec-19 18:50
mvahoney the codewitch14-Dec-19 18:50 
GeneralRe: Just because you can doesn't mean you should. Pin
CPallini15-Dec-19 5:52
mveCPallini15-Dec-19 5:52 
GeneralRe: Just because you can doesn't mean you should. Pin
englebart16-Dec-19 2:42
professionalenglebart16-Dec-19 2:42 
GeneralRe: Just because you can doesn't mean you should. Pin
Member 1181677616-Dec-19 7:41
Member 1181677616-Dec-19 7:41 
GeneralRe: Just because you can doesn't mean you should. Pin
rjmoses16-Dec-19 12:56
professionalrjmoses16-Dec-19 12:56 
GeneralRe: Just because you can doesn't mean you should. Pin
Member 1181677616-Dec-19 19:19
Member 1181677616-Dec-19 19:19 
I stand corrected. I missed the double decrement (sorry, it has been a long and intense semester, I'm not fully back up to speed). Multiple increments or decrements of the same variable are undefined according to the standard. Sequencing might make it OK, but that can't be assured. There might be a corner of the standard that allows it with the sequencing, but I wouldn't assume that it is legal.

The intent of the original logic appears to be: Given a key r, to search two hash tables, in order. If a match is found, then r should be assigned the index returned from the matching hash table, reduced by one for indexing into the "commands" table.

The difference between your code and the original is that the original reassigns "a" after the "or". That is intended to wipe out any previous changes to "a". Your code does not do that so it will potentially get a double increment.

To match the intent of the original, one of the following tweaks could be used (minor formatting applied -- note that I use "and", "or" and "not" to reduce errors caused by mixing up & and &&, or by mixing up | and ||).
action a;
if ((a = hash_table[r]) and not cmdcmp(commands[--a].name, p)
      r = a;
   else if (a = short_hash_table[r]) and not cmdcmp(commands[--a].short_name, p))
      r = a;
   else
      r = -1;
This has an extra assignment -- that the compiler can optimize away by merging identical blocks. A more interesting alternative is
action a;
if ((a = hash_table[r]) and (a = a - 1, not cmdcmp(commands[a].name, p)) or
    (a = short_hash_table[r]) and (a = a - 1, not cmdcmp(commands[a].short_name, p)))
      r = a;
   else
      r = -1;
which makes use of the comma operator. Decrement is not directly used, but the compiler will generate the same code, but lifted before evaluating the function call's parameters.
GeneralRe: Just because you can doesn't mean you should. Pin
rjmoses17-Dec-19 0:09
professionalrjmoses17-Dec-19 0:09 
GeneralRe: Just because you can doesn't mean you should. Pin
Member 1181677617-Dec-19 1:56
Member 1181677617-Dec-19 1:56 
GeneralRe: Just because you can doesn't mean you should. Pin
patbob16-Dec-19 12:23
patbob16-Dec-19 12:23 
GeneralRe: Just because you can doesn't mean you should. Pin
Member 916705716-Dec-19 22:28
Member 916705716-Dec-19 22:28 
GeneralMore 3D Printing......well, I couldn't help it. Pin
DaveAuld14-Dec-19 5:12
professionalDaveAuld14-Dec-19 5:12 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
OriginalGriff14-Dec-19 5:17
mveOriginalGriff14-Dec-19 5:17 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
PIEBALDconsult14-Dec-19 5:38
mvePIEBALDconsult14-Dec-19 5:38 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
Ron Anders14-Dec-19 5:47
Ron Anders14-Dec-19 5:47 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
honey the codewitch14-Dec-19 7:14
mvahoney the codewitch14-Dec-19 7:14 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
Nelek14-Dec-19 7:23
protectorNelek14-Dec-19 7:23 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
honey the codewitch14-Dec-19 7:24
mvahoney the codewitch14-Dec-19 7:24 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
Nelek14-Dec-19 7:26
protectorNelek14-Dec-19 7:26 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
Jörgen Andersson14-Dec-19 11:16
professionalJörgen Andersson14-Dec-19 11:16 
JokeRe: More 3D Printing......well, I couldn't help it. Pin
Daniel Pfeffer14-Dec-19 22:10
professionalDaniel Pfeffer14-Dec-19 22:10 
GeneralRe: More 3D Printing......well, I couldn't help it. Pin
DaveAuld15-Dec-19 0:35
professionalDaveAuld15-Dec-19 0:35 
GeneralDamnit - another thing dies. Pin
OriginalGriff14-Dec-19 4:20
mveOriginalGriff14-Dec-19 4:20 
GeneralRe: Damnit - another thing dies. Pin
Cp-Coder14-Dec-19 4:24
Cp-Coder14-Dec-19 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.