Pages

Saturday, August 3, 2013

Ignore Linus, C++ is fun

The War of the Roses had nothing on the language wars. Since the beginning of my computing career, I've watched developers fight over the relative virtues of programming languages... from C versus Pascal to Ruby versus Python. What is it about this subject that brings out such passion?

Linus Torvalds has gotten into the fray, posting a message on a techie list in which he says outright that C++ is a horrible language. "It's made more horrible by the fact that a lot of substandard programmers use it, to the point where it's much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C."

According to Torvalds — yes, the Linus of Linux — C++ leads to really really bad design choices. He says that developers "invariably start using the 'nice' library features of the language like STL and Boost and other total and utter crap," that may "help" you program, but they cause infinite amounts of pain when they don't work and inefficient abstracted programming models.

We all do respect this man's point of view as he has clearly proved that he know what he is doing and saying for last 2 decades. Nowadays, a good programmer is the one who know the right tool to use for each situation, including programming languages. The funny part is that i came across a C++ code snippet that I instantly wanted to share it. These very few lines here are genius!
#include <cstdio>double m[]= {7709179928849219.0, 771};
int main()
{
m[1]--?m[0]*=2,main():printf((char*)m);
}

There is absolutely no way to tell what this code is capable of when you just look at it. Calm down, it is not a virus lol. It simply outputs
C++Sucks

Magic? maybe! I will quote dasblinkenlight's explanation for this:
The number 7709179928849219.0 has the following binary representation as a 64-bit double:
01000011 00111011 01100011 01110101 01010011 00101011 00101011 01000011
+^^^^^^^ ^^^^---- -------- -------- -------- -------- -------- --------

+ shows the position of the sign; ^ of the exponent, and - of the mantissa (i.e. the value without the exponent).

Since the representation uses binary exponent and mantissa, doubling the number increments the exponent by one. Your program does it precisely 771 times, so the exponent which started at 1075 (decimal representation of 10000110011) becomes 1075 + 771 = 1846 at the end; binary representation of 1846 is 11100110110. The resultant pattern looks like this:
01110011 01101011 01100011 01110101 01010011 00101011 00101011 01000011
-------- -------- -------- -------- -------- -------- -------- --------
0x73 's' 0x6B 'k' 0x63 'c' 0x75 'u' 0x53 'S' 0x2B '+' 0x2B '+' 0x43 'C'

This pattern corresponds to the string that you see printed, only backwards. At the same time, the second element of the array becomes zero, providing null terminator, making the string suitable for passing to printf.

Such code cannot be useful, it is more accurate to call it "useless headache" but it is still funny! Note that i am not judging such a huge programming language nor agreeing with Linus Torvalds, I believe in diversity and that every tool has its own use, but it seemed funny to share this. Subscribe to this blog and follow me on twitter to get more updates in the computer world.

6 comments:

  1. I had the idea to run it on my computer but my bash window was connected to a PowerPC Mac as I was doing endianness tests. This gave me skcuS++C�� :p

    ReplyDelete
  2. you're absolutely right, I was trying to say that I recently came across it lol

    ReplyDelete
  3. I'm not skilled in C++ but i've changed cstdio to stdio, I've got something wrong with the first one and i got it right http://i.imgur.com/7MNK1LP.png

    ReplyDelete
  4. The only C++ in this code is the cstdio. If you replace it with stdio.h you have 100% C. Which means that you use C to show that C++ is funny? Don't get your point. :)

    PS: I like C++. You get a fast binary without the need to implement every stupid container by yourself.

    ReplyDelete
  5. yeah you'r right, I just couldn't find a funnier way to introduce endianness for those who don't know it :)

    ReplyDelete