|
Both the operator precedence (and thus the order of evaluation) and short circuiting are part of the C language definition and is not compiler dependent. The compiler can only reorder the expression during optimization if it can ensure that it does not change the result.
It is quite safe to rely upon. This expression already has a sufficient number of parentheses, more just makes it less readable. I'm not even sure where you would even put extra parentheses.
|
|
|
|
|
I read the spec and it supposed to be standard across all compilers but I have lost count of how many OS and application bugs of that type I have had to chase.
As patbob pointed out, it is the pre-decrement that is worrisome. And the first couple of times I look at the code, I missed the "!".
To test this logic, I wrote a little program which (I think) simulates the original code results:
#include <stdio.h>
int testf(int rv, int a) {
return rv;
}
int main()
{
int value = 0;
for (int i = 0; i < 16; i++) {
printf("%2x = ", value);
int a = 0;
if (value & 8) printf("T"); else printf("F");
if (value & 4) printf("T"); else printf("F");
if (value & 2) printf("T"); else printf("F");
if (value & 1) printf("T"); else printf("F");
printf(" = ");
if ( (value & 8) && testf((value & 4), --a) || (value & 2) && testf((value & 1), --a))
printf("T %d", a);
else
printf("F %d", a);
value++;
printf("\n");
}
return(0);
}
The results are:
0 = FFFF = F 0
1 = FFFT = F 0
2 = FFTF = F -1
3 = FFTT = T -1
4 = FTFF = F 0
5 = FTFT = F 0
6 = FTTF = F -1
7 = FTTT = T -1
8 = TFFF = F -1
9 = TFFT = F -1
a = TFTF = F -2
b = TFTT = T -2
c = TTFF = T -1
d = TTFT = T -1
e = TTTF = T -1
f = TTTT = T -1
|
|
|
|
|
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.
|
|
|
|
|
You're right--I missed that second assignment. Thank you.
And this kinda goes to my point: Well written code should be easily comprehensible to us mere mortals.
While the original code brilliantly takes advantage C language capabilities, it lends itself to errors of understanding, especially when a person doing maintenance is under pressure to fix a problem. (We lose approximately 20% of our thinking capabilities under pressure and, under sufficient pressure, the logical, thinking part of our shuts down.)
|
|
|
|
|
You are absolutely correct. Both of us missed parts of the expression. That is why I would more likely use the first variant I gave rather than the second one. And probably would write neither in practice, but without a wider context can't say what I would actually write. It would probably involve some restructuring.
|
|
|
|
|
rjmoses wrote: Somebody sure put a lot of faith that the order of evaluation, especially short-circuit evaluation, would remain the same across compilers! Order of evaluation and the short-circuit behavior is (or at least, used to be) part of the C language definition.
It's the use of variable assignment and pre-decrement operator in the (short-circuited) if statement that worries me. Regardless, I hope you rewrote it into something more maintainable.
|
|
|
|
|
I remember a demo where someone turned a for-loop into an until one (ptr->next instead of i++, next!=nil instead of i<=max and so on). My first reaction was "wow, that's clever". My second reaction was "you know how you f***ing hate it to debug clever code, especially if it's your own clever code from half a year ago".
|
|
|
|
|
After having received the anycubic mega-s and being really impressed with it, I found that I was wanting to rattle out maybe another small print, but the printer was busy doing something big, so basically tied up for the day (or over night). I started looking around for a 2nd smaller printer, one of these nano/mini ones that could print objects upto around 10cm cubed. The prices weren't far off the same price as my current printer, and by the time I added shipping to Cyprus, it just didn't make sense, so I didn't bother.....
Then I was in chat on a thread in Twitter, and someone posted a link to the printer I had already bought, on the manufacturers website and had mentioned the price, and I thought that it sounded slightly cheaper than normal and so out of curiosity I clicked the link and visited the site, and sure enough it was on sale $40 off. That is when I noticed they were having a pre-christmas sale.
I just started looking at all their models again, just because I was the site, and then I noticed their large Delta style Predator printer was reduced from $619 to $339 in the sale, but only available in Europe and Australia. I thought their shipping price to Cyprus would kill the savings. I added it to the basket and took it through the checkout process to see what they would charge for the shipping and well elephanting me it was FREE!
I really couldn't miss this, so paid with paypal and the exchange rate came out at 266ukp. So I consider this a birthday present to myself, seeing as that is next week!
Now, I knew this thing would be bigger than my current one, as the print volume is 370mm diameter by 455mm tall, but it wasn't until I looked at Youtube at the reviews that it really dawned on me how big.
I mean, it looks massive next to this guy....
Anycubic Predator 3D printer Review - YouTube
Good job I have a dead corner in the office at home that it can sit in!
Anyway, can't wait for it to arrive now, could job I'm heading home next week and it should be arriving around the same time.
|
|
|
|
|
Holy cr@p, that's big!
I'd have to remove stuff from my office just to get it in, and even then it'd be massive cat-bait unless I moved even more stuff out to give space for a box to put it in ...
Congrats on the free shipping though!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
As absolutely bitchin and "space age" 3d printing is. Compared to laser 2D printers it still seems primitive. Lots of plugging stuff and add on stuff to make it better. Reminds my of the early days of the personal computer like the Amiga heyday and the nerd clubs. I think someday we'll look back and snicker at the fiddlyness of it all.
Kick A$$ tugboats.
Toot toot!
|
|
|
|
|
Print me a cute lil car.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I think it could manage this one:
cute lil car - Google Search[^]
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Wow, that looks like what you get if you fart inside a Smart car.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
IMCDb.org: BMW Isetta 300 in "Family Matters, 1989-1998"[^]
Steve Urkel loved it
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
DaveAuld, won't you print me a Mercedes Benz ?
My friends all drive Porsches, I must make amends.
Worked hard all my lifetime, no help from my friends,
So, DaveAuld, won't you print me a Mercedes Benz ?
With apologies to Janis Joplin
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
|
I thought the chicken was rather cool and a little odd textured last night - I cooked a Red Thai Curry in the Sous Vide - and I checked it today: the temperature gauge is overreading by between 15C and 25C. So last nights chicken was cooked at ~40C for three hours instead of 65C ... fortunately, we don't seem to have developed any "problems" as a result, which is definately good news!
So, contact the manufacturers to see if I can fix it since they are in the US ... and then realize it's got a two year warranty and I ordered it direct from them in March 2018! Yay!
It should be covered!
Then realize there is no way it's going to be fixed under warranty before Christmas, and I have based the whole meal around Sous Vide turkey and trimmings.
So, I'll get it fixed for free, eventually, but I'll have to buy a "spare" to tide me over while I wait ... damnit!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Quote: Damnit - another thing dies
Please choose your words with care. You just made me feel my pulse. I thought for a moment you're referring to me.
|
|
|
|
|
Fire up the barbie on Christmas Day.
btw four installed but not us
|
|
|
|
|
Chris has a ... unique grasp of time, I've noticed!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I wonder if he's a member of one of those tribes that talk about "the dream time"?
|
|
|
|
|
you mean it wont let you set it to 90 degrees? (minus the 25 error = 65)
or just pull out the element from an old electric jug,
cut 1 side of the power cable and add a huge resistor,
drill a hole in the sous vide
and shove that in there to add the extra heat.
may require a bit of trial and error on the resistor.
oh, and if no resistors on hand use a bucket of water,
just add salt or fresh water to adjust resistance.
(I've always done my most ambitious DIY during Christmas.)
<< Signature removed due to multiple copyright violations >>
|
|
|
|
|
If the temp sensor isn't accurate it will wreck the food - the margin for "which protein to cook" is pretty narrow, and it makes a huge difference to the end result. A bit like the difference between medium rare and well done - and for the same reasons.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|