Sometimes working with an old c compiler brings up painful, yet fun-filled days of making it speak the same language as you, and sometimes you just go “*$&%*$#&% you piece of sh** compiler”
I had one such moment today, and after I had returned to Zen (some soul-searching and some google searching), I realized it quite simple.
$ gmake
...
...
some_file.h:42: badly punctuated parameter list in `#define'
some_file.h:64: badly punctuated parameter list in `#define'
Failed to compile
$
This was caused by the following:
file: some_file.h
-----------------
...
...
#define LOG(...) {\ printf (##__VA_ARGS__); \
}
...
...
This works in most modern C compilers. However, if you happen to use one from the 80s (I exaggerate a little – not by much), it throws the error as above. You can fix this as follows:
file: some_file.h
-----------------
...
...
#define LOG(ARGS...) {\ printf (##ARGS); \
}
...
...
Source : http://nixscripts.blogspot.com/2010/09/badly-punctuated-parameter-list.html
Posted by Shiva at 6:24 pm on November 8th, 2011.
Categories: Tech. Tags: #define, badly punctuated parameter list, c++, code, compiler issues, Development, gcc, programming.
It’s been a while since I’ve done anything except write code — lots of it. The last 20 days have been insane, and ofcourse to a take a break from writing code, I like to read code that others write. (You DO know that I’m crazy, right?!). In one of my futile attempts at clearing my google-reader reading list, I chanced upon a post by Veerabahu, on finding if a number is a power of 2 (or not).
As he writes, there is the simpleton O(n) solution (you will have to click-through for that), and the most elegant (yet) bitwise solution:
/*
*/
bool
is_power_of_2(int n) {
return ((n & -n) == n);
}
The bitwise way to calculate the power of 2 is probably the most efficient in c like languages. Ofcourse for that, your language of choice needs to support it and should be more efficient that common math functions. The other way is to use some simple math.
Let’s say N, is the value, and you need to check if it is a power of two. Compute n = log N / log 2. If floor(n) == n, then N was a power of 2.
/*
*/
bool
is_power_of_2_pure_math_baby(int n) {
/* address -ve numbers */
if (n < 0)
n = -n;
double i = log(n)/log(2); /* i = power of 2 */
return (lower(i) == i); /* check if perfect power of 2 */
}
This is obviously, a less efficient way of checking if a number is a power of 2, than using the bitwise method. However, it has a few advantages:
- It works exactly the same way for all values of n.
- It works exactly the same way for all integers (ie, n can be int8/16/32/64, long, signed or unsigned, and the same logic would work
- It is O(1) like the bitwise solution
- It is less cryptic (ie just basic understanding of math is reqd for grokking this solution)
- Finally, it can be extended in future to calculate if n is a power of *any value*, not just 2
Of course, the point Josh Bloch was making in interviewing engineers, was that he is interested in knowing the WHY of a solution. It does not matter if the algorithm is marginally less optimal or different. If you are an interviewer in your organisation, and you catch yourself asking a question like this, remember that if an engineer can reduce O(n) to O(1), stop with similar micro-skills test. Find out why she coded it the way she did. It will tell you a lot more about her skills than some algorithms/tricks that can be picked up in a couple a days, if not overnight.
Posted by Shiva at 1:47 am on November 3rd, 2010.
Categories: Tech. Tags: algorithm, c++, code, power of 2, programming, puzzles.
I got really sick of using Adobe AIR apps that take up more and more of system memory for an app that does twitter.
I’ve decided to put my “coding for myself” hat on and write a QT based C++ application (that should be cross-platform). No name as yet, but I will find something soon. QtTwitter is taken. Mebbe, once I have a working version, I can get ownership of the project on google code.

That’s the proposed GUI. Sorry for the bad light. The iphone cam’s not too good with tungsten light.
Any suggestions?! I will try and post some mock up as I create em. I don’t want to be writing my own twitter REST c++ library. Any suggestion are welcome.
Posted by Shiva at 5:58 pm on October 25th, 2008.
Categories: Tech. Tags: c++, coding, native twitter client, qt, Tools, Twitter.