Shuva's blog
Declaring variables in C++ : Style or efficiency 
Wednesday, June 25, 2008, 11:54 AM - Programming
Did you know that C++ allows you to declare variables anywhere in the code and not necessarily at the beginning of the function? Of course you do! If you dont you are at the wrong place in the internet. Get away from my blog --- Shoooooooo!

Most C++ programmers who come from C background, however many times prefer to declare variables in the beginning of the function. They say its their style. They say it makes code look better. They don't want to have variable declarations scattered all around. If you are among them, think again. This feature was not introduced for mere flexibility of a programmer's style.

Your program is likely to perform better if you delay the declaration of your variable as late as possible.


An object/variable in a function may be unused because of a possible early exit, an if-then-else condition or because of an exception. Declaring late avoids unnecessary constructions of objects which may not be used in the function.

Moreover by declaring all objects, destructors of the unused objects are called unnecessary.

Additional tip:
If you face a compiler error is declaring an object inside a "case" of a switch statement use an extra "{" "}" pair as shown below:

    switch(action) {
case WRITE:
Writer writer;
int x;
writer.Write("Some stuff");
break;
case READ:
Reader reader;
reader.Read(data);
break;
default:
break;
}


You may get an error saying "initialization of 'writer' is skipped by 'case' label" if you have defined a constructor for Writer or Reader class. The fix is below:

    switch(action) {
case WRITE:
{
Writer writer;
int x;
writer.Write("Some stuff");
}
break;
case READ:
{
Reader reader;
reader.Read(data);
}
break;
default:
break;
}

Happy Programming.//

add comment ( 117 views )   |  0 trackbacks   |  permalink   |   ( 3.1 / 34 )
The new del.icio.us plugin for Firefox 3 
Tuesday, June 24, 2008, 08:57 AM
When I upgraded to Firefox 3 a few days back, I was disappointed to see that there exists no Add-on for del.icio.us. But then today I found patience was truly rewarding. The del.icio.us guys have done a wonderful job in designing the new Add-on. It makes del.icio.us more useful than it was ever before. Check out the new features here.
add comment ( 111 views )   |  0 trackbacks   |  permalink   |   ( 2.9 / 30 )
All Sysinternals tool on shared drive 
Friday, June 6, 2008, 10:08 AM - Tips and Tricks
Powered By ReadTheWords.com
All Sysinternal tools are not available on a shared drive at \\live.sysinternals.com\tools

If you have a super fast internet connection, you can probably run it live from there, with some acceptable risks, but you could just dump them all locally like I did a few minutes back.

Happy Debugging.//
add comment ( 2055 views )   |  0 trackbacks   |  permalink   |   ( 3.2 / 37 )
Best C++ coding standard I have ever read 
Monday, May 26, 2008, 11:55 AM - Programming
Powered By ReadTheWords.com
A good C++ coding standard is one which tell you when you may break the rule. For that you need to un-satisfy the justification of the recommendation. In other words, one quality of a good coding standard is one which gives a justification for every item.

Secondly, it should contain language specific suggestions and guidelines (stuff which are legal, but best avoided outside your college/school).

C++ Coding Standard by Todd Hoff is the best I have ever read. Where was it all the time?

Happing coding.//
add comment ( 154 views )   |  0 trackbacks   |  permalink   |   ( 2.9 / 39 )
Accidental Learning 1 : Visual Studio 2005 (VC++) Tips 
Friday, May 16, 2008, 01:03 PM - Tips and Tricks
Powered By ReadTheWords.com
Shuva learned the following stuff by accident just yesterday:

1. If your braces and alignment in a block of code is not proper, the do a Ctrl-X, Ctrl-V (cut the code block and paste back). Voila!!

2. Pressing F-12 is equivalent to "Go to Definition".

Happy programming.//
1 comment ( 228 views )   |  0 trackbacks   |  permalink   |   ( 3.1 / 33 )

<<First <Back | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | Next> Last>>