## [C++] Interesting example of C++ code obfuscation

Recently a friend of mine has given me a piece of code in C++: puts(&3["abcdef"]);puts(&3["abcdef"]); At first glance it seems to not compile at all. But not only it compiles, but also prints “def”.

Tag: code

Recently a friend of mine has given me a piece of code in C++: puts(&3["abcdef"]);puts(&3["abcdef"]); At first glance it seems to not compile at all. But not only it compiles, but also prints “def”.

If we want to know the last ten digits of number an – we have to evaluate expression an mod 1010. Using brute force approach, we have to do O(n) ( If you don’t understand big „O” notation, visit: Big O notation – it’s VERY important) multiplications and modulo divisions. When n is greater than 109, it will take a lot of time. Fortunately, we can use more „mathematical” approach, which has O(log n) complexity.

In PHP we can use function eval in order to evaluate given variable (string) as PHP code: <? $var = ‘for ($i=0;$i<5;$i++) { echo $i.\'<br>\’; }’; eval($var); ?><? $var = ‘for ($i=0;$i<5;$i++) { echo $i.\'<br>\’; }’; eval($var); ?> output: 0 1 2 3 40 1 2 3 4