JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
Congratulations! | Reply
The article looks extremely helpful for all of us that are trying to get a good job/internship! Thanks!

I missed the virtual keyword which is quite usual in the interviews, but I guess you left that for part 2 :)! I am already waiting for it!
Re: Congratulations! (response to post by tywok) | Reply
Article is good, thanks. i would like to see in future article
some critical words about #define. Work of this code
compiled in linux is differs from others.

int x = 3;
if (-10 * abs(x - 1) == 10 * abs(x - 1))
printf("wrong\n");

you can test it arena. this code writes "wrong"
it because
#define abs(x) ((x)>0)?(x):(-(x));
and precompiler replace "abs(x - 1)" with "((x - 1)>0)?(x - 1):(-(x - 1))"
-10 * ((x - 1)>0)?(x - 1):(-(x - 1))
the priority is of * is greater then ? and we have
(-10 * ((x - 1) > 0)) != 0) ?(x - 1) : (-(x - 1))
answer will be x - 1
for left part we have the same situation and its equals x - 1

please describe this bug in your future article
my post will see much less people then your article
Re: Congratulations! (response to post by Alias_Prudaev) | Reply
Yeah macros are tricky to get right. In this case the bug is in the way the macro is written, which ought to be
#define abs(x) ((x) > 0 ? (x) : (-(x)))

i.e., another layer of brackets on the outside. It's still bad that it's a macro at all in this day and age though, since if "x" is an expression with side effects then it will be evaluated multiple times. GCC has some extensions to get around this (statement-expressions), but it really ought to just be a static inline function.
Re: Congratulations! (response to post by bmerry) | Reply
see there exist error in your code
wrong:
#define abs(x) ((x) > 0 ? (x) : (-(x)))
right:
#define abs(x) (((x) > 0) ? (x) : (-(x)))
all is as i said. marcos is bad.
Re: Congratulations! (response to post by Alias_Prudaev) | Reply
It's a known bug in GCC code generator, and has nothing to do with macros.
The workaround is to disable builtin abs() function with -fno-builtin-abs option, or avoid multiplying abs() result by negative constant.
Re: Congratulations! (response to post by tywok) | Reply
Thanks, many thanks!

I'm glad this article has reached its purpose, that is helping all of us getting a better job :).
As for "virtual", yeah you're right, stay tuned for the second part!

Many thanks and bows once again for bmerry, kyky and sql_lall
Re: Congratulations! (response to post by Alias_Prudaev) | Reply
I also received some other feedback about "untouched issues". If the guys from the TopCoder editorial section will agree I'll prepare a follow up to this series of two articles.

Thanks for the appreciations!
RSS