Welcome to MSDN Blogs Sign in | Join | Help

My travels with WDF

The continuing story of a boy, his dog and their discovery of the world outside...of WDM.
This blog is reader supported

Thanks to the intrepid reader, TJ, who notified me that NHL Center Ice was now available on FiOS TV.  After 14 years of service, I will bid a adieu next Friday to DirecTV.  For those of you wondering why I've been touting FiOS, it's currently the only TV service that does not recompress their HD signals before throwing it down the wire.  Well, other than OTA, but show me where I can get Montreal Canadiens or San Jose Sharks games in HD via OTA in Seattle.


Just so this post isn't devoid of anything other than another gloat, here's something we were just throwing up to stumble each other;

 

void
main ()
{
    int * m;

    m [1,2] = 5;
}

 

This does what?

Posted: Friday, November 14, 2008 5:39 PM by patman
Filed under: ,

Comments

strik said:

It invokes UB (undefined behaviour). ;)

I assume this is C, not C++ - not all of my comments apply to C++.

void    /* 1 */

main () /* 2 */

{

   int * m;

   m [1,2] = 5; /* 3 */

}

Ad 1: In C, void main ... is not a legal prototype. It should be int main ... instead

Ad 2: In C, void main() and void main(void) are not identical. The first is an incomplete prototype, while the second form is a complete one. The latter is preferably (but: Cf. ad 1, better is: int main(void))

Ad 3: a. Using an initialised pointer

b. m[1,2] is "reduced" to m[2]. Thus, this is equal to "m[2] = 5;"

  Why? The comma operator "executes" all operands  of it (that is, "1" and "2"), and "returns" the result of the second operand - in this case "2". As the operands do not have side-effects, m[1,2] is identical to m[2]. But, because of 3a., it is still UB.

I assume you only wanted to hear 3b, but this way, it is more complete. ;)

# November 17, 2008 7:31 AM

patman said:

hahaha, nice and thorough!

Yes, 3b is what we were screwing around with, sans W4 warnings enabled, it compiles, which is just bonkers.

For a bonus point;

what does

int i = 99;

m[--i,--i,--i] = 5;

give us?

# November 17, 2008 3:27 PM

strik said:

This extra code is "like"

int i = 99; i -= 2; m[--i] = 5;

or even

int i = 99; i -= 3; m[i] = 5;

Why? Normally, in C, if I modify a variable multiple times in a statement, the outcome is undefined - that is, the compiler can generate "anything it wants" out of this. For example, something like

a = ++i + ++i;

or, even worse,

i = ++i + ++i;

is undefined.

In the case of your example, at a first glance, "--i,--i,--i" seems to be undefined, too. However, it is not. Why? Because the comma operator is a so-called "sequence point".

What is a sequence point? A sequence point is a point in the execution of the abstract machine where all side-effects of previous operations have already occurred.

The "+" is no sequence point, and a "=" is not, either.

The most commonly known sequence point is the semicolon after statements.

Another one - and this is important in your example - is the comma separator (more precisely: After the execution of the left-hand side of the comma operator, there is a sequence point). So, when the second "--i" "executes" (in the abstract machine defined by C), the first "--i" has already occurred. When the third "--i" "executes", the second one has already occurred, too.

# November 19, 2008 7:04 AM

patman said:

You're taking all the fun out of this by being too smart. :)

# December 1, 2008 3:56 PM
Anonymous comments are disabled
Page view tracker