|
C# is the same, so, it seems this behaviour is predictable and I will have to read PIEBALD's lengthy excerpt.
|
|
|
|
|
What's happening makes sense. The operator in question is the postfix incrementor; it increments the variable, but it returns the pre-incrementation value. So, it increments itself, but it's being assigned its original value.
|
|
|
|
|
Isn't the question just whether the runtime environment first assigns the old value. I really thought this would just eventually increment the variable. Because it should normally first assign the old value to i, and then increment i. Why should it ever store the old value somewhere first, then increment i, then put that old value back in i? I know I'm wrong, cuz as said before C# gives an infinite loop, but still it's weird.
|
|
|
|
|
How could it do that? The operator's function has to end (return) before the assignment occurs; that means the incrementation has to happen first.
|
|
|
|
|
liquidplasmaflow wrote: the incrementation has to happen first
says who? the autoincrement is not necessary in the expression evaluation, and hence it can
be scheduled before or after the assignment operator, that is why the net result is undefined.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
C++'s operator ++ is a function. Last I checked, a function can't do anything after it returns
<br />
int& operator++(int& argument, int)<br />
{<br />
int return_value = argument;<br />
argument += 1;<br />
return return_value;<br />
}<br />
Am I right?
|
|
|
|
|
Hi,
the autoincrement operator may be implemented as a function, I don't
think it has to; in C it typically is not. And even when it is a
function, it could be inlined automatically, and the instructions
then can be rescheduled by the compiler, so there is no way you can
predict which one (the final store, or the autoincrement) will
occur last and hence prevail; all that in accordance with the
legalese language specs PIEBALD showed us.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I'd be interested to know how early versions of C would handle it.
I expect the increment should happen first (if indeed it's handled by a hardware instruction), then the original value assigned back, yielding the infinite loop.
|
|
|
|
|
I have used dozens of C compilers over the years, most if not all of them did not care about
the ambiguity, but simply evaluated the entire expression including side-effects before storing
the result. It is only with the advent of RISC and advanced instruction scheduling that
things got unclear, and the undefined stuff got introduced.
Anyway, I learned long ago not to write code that would be ambiguous or just difficult to read
and understand, so it never really mattered ... What is the point of studying a page of rules
to make sure something is/isn't ambiguous or undefined, where one could add an intermediate
statement or a pair of parentheses? It isn't APL or Lisp after all.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
It seems assigning the value of an incremented variable to itself overrides the increment operation.
If not, it wouldn't actually matter in which order the operations are executed as the result would be the same:
increment comes first:
i = 0 // i = 0
i++ = 1 // i = 1
i = i // i = 1
result: 1
assignment comes first
i = 0 // i = 0
i = i // i = 0
i++ // i = 1
result: 1
|
|
|
|
|
I agree. If the code would look like this:
a = i++;
Then a would be assigned the value of i and i would be incremented after that, so I would expect in given case that in first iteration i would be assigned value of 0 then i would be incremented. Weird indeed.
modified 19-Nov-18 21:01pm.
|
|
|
|
|
Today, I was reviewing the code of a particular module that a vendor who I had outsourced the product had delivered. There was one interesting line. He is reading input from .NET Cache. Good! He is trying to enhance the performance instead of making a costly remoting call.
He is also making a not null check after getting the elements from cache. It sounds still good right?
Well! Now the fun is; there is nothing to handle when cache is null. When I contacted him for this reckless coding horror, the coolest reply was 'IIS needs to be reset when cache gets emptied '.
Is guillotine available in India?
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson
|
|
|
|
|
Vasudevan Deepak K wrote: Is guillotine available in India?
That's too humane. North Korea is always available as a harsher alternative.
ROFLOLMFAO
|
|
|
|
|
another example of promising the earth and cutting quality corners to meet the targets.. its just a case of a "developer" being lazy
|
|
|
|
|
PeterTheGreat wrote: another example of promising the earth and cutting quality corners to meet the targets.. its just a case of a "developer" being lazy
Yes, and probably a you-get-what-you-pay-for kind of thing, too.
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
This guy walks into a bar and...
|
|
|
|
|
Vasudevan Deepak K wrote: When I contacted him for this reckless coding horror, the coolest reply was 'IIS needs to be reset when cache gets emptied'.
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
I cant tell you how many of these ive cleaned up in recent months:
if(HttpContext.Current.Cache["someKey"] != null)<br />
{<br />
SomeClass someClass = (SomeClass)HttpContext.Current.Cache["someKey"];<br />
}
The problem being that occasionaly the cache item is expired between lines 1 & 2 leading to rather hard to spot null instance exceptions when trying to use someClass.
|
|
|
|
|
Within one of my company's XSLT document's, I came across the following. Seems to be an XSLT version of a familiar coding horror...
<xsl:for-each select="fl:values/fl:dataItem[@field='fl_name']>
<xsl:if test="position()=1">
Output Here
</xsl:if>
</xsl:for-each>
|
|
|
|
|
From time to time it is necessary for me to code xslt. Each time is a horror.
the horror
the horror
|
|
|
|
|
Personally, I'm torn. I do like the declarative nature of specifying transforms on XML documents. However, two problems remain:
1. XML. Fine for structured data passing between systems. I use it regularly for these purposes. However, as a programming language, I'd say it leaves something to be desired. I'm a C-family developer (C++, Java, C#) and appreciate the terseness, so XSLT makes me shudder with its verbosity.
2. As with many scripting languages, if used well code is fairly readable, but put it in the hands of a less capable developer, and the source is virtually unreadable.
Obviously what we need is an XSLT transform to transform badly written XSLT into well-written XSLT , and a transform to render (and the reverse) XSLT to more user- (i.e. developer) friendly syntax.
|
|
|
|
|
Well, I don't create XSLTs on a daily basis, only once a month. Each time I have to go back to look at already created ones to see how to do it. It just doesn't seem natural to me, especially dealing with variables and parameters.
When I look at XSLTs in bigger projects that have been worked on for a while (by other people) - it makes XSL to look like a self-obfuscating language. People tend to put the whole application into one file (doesn't matter if it got 10.000 lines), do not properly group things, use copy & paste and the rest is done by the overhead of tags and symbols through XML.
It makes me think XSL is generally treated as throw-away code, like prototypes going into production.
|
|
|
|
|
Doc Lobster wrote: People tend to put the whole application into one file (doesn't matter if it got 10.000 lines), do not properly group things, use copy & paste and the rest is done by the overhead of tags and symbols through XML.
Ouch. Well, slap 'em around a bit. There's just no excuse for that.
|
|
|
|
|
Doc Lobster wrote: From time to time it is necessary for me to code xslt. Each time is a horror.
Eh, not so sure about that. Sure, the syntax is horrible, but the semantics make sense IMHO.
|
|
|
|
|
From time to time, it is necessary for me to review procedural XML translation code from people who don't know how to use XSLT. Each time is a horror...
But who is the king of all of these folks?
|
|
|
|