What Happens When We Lie to the Compiler?
Posted on November 13th, 2009 at 12:57 by fr3@K

When you lie to your compiler, don’t be surprised to get results those differ from what you expect.

In this case, the coder promised his/her compiler the value of “value” won’t be changed, as it was declared const.

Therefore our best friend (yes, the compiler) who trusted the coder, thought “hey, I could cache this value in a register and not fetch it from RAM every time I reference it”, i.e. optimization.

If we change the code in the link slightly, so that function funcC would print *value before the assignment, one would see that the value of *value had actually been modified by function funcA. However, the modification was not visible in main, because the compiler was lied to as the coder explicitly told it that there would be no modification to value.

In the body of function funcC, though value was declared as a pointer to const instance, the compiler was smart enough to know it was lied to. There was a const_cast and a assignment, therefore, the modification to *value was reflected in the output.

del.icio.us:What Happens When We Lie to the Compiler? digg:What Happens When We Lie to the Compiler? spurl:What Happens When We Lie to the Compiler? newsvine:What Happens When We Lie to the Compiler? furl:What Happens When We Lie to the Compiler? Y!:What Happens When We Lie to the Compiler? 黑米共享書籤:What Happens When We Lie to the Compiler? 推推王:What Happens When We Lie to the Compiler?
Texas Army Base Rampage
Posted on November 6th, 2009 at 7:11 by fr3@K

我的媽呀~~

在舊金山機場等回台北的飛機, 電視上不斷的播放著德州美軍基地內部發生槍擊新聞.

CNN 目前的說法是: 連同兇手總共掛了 12 個人, 兇嫌是一名現役中校.

del.icio.us:Texas Army Base Rampage digg:Texas Army Base Rampage spurl:Texas Army Base Rampage newsvine:Texas Army Base Rampage furl:Texas Army Base Rampage Y!:Texas Army Base Rampage 黑米共享書籤:Texas Army Base Rampage 推推王:Texas Army Base Rampage
So, You Think C++ is Slower than C
Posted on November 6th, 2009 at 2:01 by fr3@K

It is true that C++ provides programming constructs those consume more CPU cycles than it would when done without higher level of abstractions. But, hey, that’s the cost of abstractions, why pay when they are not needed?

Stop laying out your C++ design like Java. Avoid inheritance when possible. Don’t make every member function and destructor virtual without really thinking. They not only increase your run time overhead, they boost the complexity (read coupling) of your code like nothing else.

C++ does every C-style stuff the same way as C does. Not to mention it is capable of more compile time computation than C. I just fail to see how C++ could be slower than C in run time, seriously.

del.icio.us:So, You Think C++ is Slower than C digg:So, You Think C++ is Slower than C spurl:So, You Think C++ is Slower than C newsvine:So, You Think C++ is Slower than C furl:So, You Think C++ is Slower than C Y!:So, You Think C++ is Slower than C 黑米共享書籤:So, You Think C++ is Slower than C 推推王:So, You Think C++ is Slower than C