How to Remove Unreferenced Formal Parameter warning?

26 11 2008

Hands on legacy code base is a different kind of game! You have to learn a lot of tricks. During old days, legacy codebases have the compiler default settings. But now, for better error catching, most will switch  the warning level to high. Well, did you ever ported your legacy codebase by updating its project settings such as by switching the warning level from Level 3 (default) to Level 4, most probably you’ll get this – “Warning C4100: ‘Param2’ : unreferenced formal parameter”.

In most of the case, it might be some unused stack variable. But in some other cases, it might be the unused function parameters as well. For instance, if base class have some pure virtual function, we have to implement that, even if we don’t have any specific implementation for it. In that case, the parameters will not be used and can cause the same warning. Well how to “kick out” those warnings easily?

unreferrencedvariablewarning


Well, there are two tricks.

1) Use the macro – UNREFERENCED_PARAMETER
The common trick is to use the macro UNREFERENCED_PARAMETER(). When it expands, it just assigns the variable to itself. Just like a=a. Which will remove the warning, since the variable is used to assign to itself.

// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool Param1, bool Param2 )
{
    // Here Param1 and Param2 are not used which will
    // trigger warning. Now they won't.
    UNREFERENCED_PARAMETER( Param1 );
    UNREFERENCED_PARAMETER( Param2 );
}

2) Comment out the variable names.
Even though UNREFERENCED_PARAMETER() serves the need, its not efficient. isn’t it? It assigns the variable to itself and thus avoids the warning. Well there is another easy method. Just comment out the variable name. Check the code snippet below.

// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool /*Param1*/, bool /*Param2*/ )
{
    // Since the variable name doesn't exist, how compiler can
    // complain that variable is not used. ;)
}


In one of my projects, I was forced write an empty implementation for a pure virtual function in derived class. And my client needs zero compilation warning. At that time I really struggled to remove this warning. 😀 Now when i shared it with you, it feels good!


Targeted Audiance – Beginners.





How to Rename Namespace?

23 11 2008


Namspaces are introduced to have logical grouping of classes. But as the framework grows you could often find that the namespace length grows which make the usage more difficult.

renamenamespaces

For instance, one namespace i’ve encountered is like this –

namespace Company
{
    namespace Product
    {
        namespace Communication
        {
            namespace Event
            {
                class CEventEx
                {
                };
            };
        }
    };
};

How much I’ve to go inside to address a class? Well, in big frameworks this kind of deep hierarchy is unavoidable. But, its a fact that it causes trouble to developer who uses it. Well, is there any idea to avoid that?


Well, you can rename the namespace to much more smaller one! Have a look at the code snippet below.

// Rename the namespace
namespace ThirdPartyEvent = Company::Product::Communication::Event;
...
// Create the object by using renamed namespace.
ThirdPartyEvent::CEventEx objEvent;


There are still a lot of gems inside C++. isn’t it? 😉


Targeted Audience – Beginners.





Manage your Constant’s List More Easily By using Enums.

18 11 2008


List of constants and their count. Every programmer have used it atleast once in his life time. A list of constants can be any for instance, if its an image processing software, it can be a list of image types, the application can manage. And we also used to keep the count of constants present.

manageconstantlistasenum2

Yes. I know you need an example. Well, lets take the image processing application itself. It might keep a constant list as follows.

const int IMAGE_TYPE_BMP = 0;
const int IMAGE_TYPE_JPG = 1;
const int IMAGE_TYPE_GIF = 2;
const int IMAGE_TYPE_COUNT = 3;

Think what all you’ve to do if you need to add a new image type? You’ve to insert the constant at end, then update the count. Like this,

const int IMAGE_TYPE_BMP = 0;
const int IMAGE_TYPE_JPG = 1;
const int IMAGE_TYPE_GIF = 2;
const int IMAGE_TYPE_JPG_LOSSY = 3;
const int IMAGE_TYPE_COUNT = 4;

If you want to append the variable, its okay. You just have to append at end and update the count. But what if you want to insert the variable at middle? In this case JPG_LOSSY seems to be more good just after the JPG. isn’t it? In that case you’ve to update all following constants.Like this,

const int IMAGE_TYPE_BMP = 0;
const int IMAGE_TYPE_JPG = 1;
const int IMAGE_TYPE_JPG_LOSSY = 2;
const int IMAGE_TYPE_GIF = 3;
const int IMAGE_TYPE_COUNT = 4;

Assume if your constant list have 50 items! Well, is there any easy trick?


Yes! You can use anonymous enums to avoid the headache. Just declare the constants as follows.

enum
{
IMAGE_TYPE_BMP = 0,
IMAGE_TYPE_JPG,
IMAGE_TYPE_GIF,
IMAGE_TYPE_COUNT
};

And if you want to insert another constant at middle, just insert it. You don’t have to modify anything else. For instance,

enum
{
IMAGE_TYPE_BMP = 0,
IMAGE_TYPE_JPG,
IMAGE_TYPE_JPG_LOSSY,
IMAGE_TYPE_GIF,
IMAGE_TYPE_COUNT
};


Since, they are anonymous enums, you can use the constant name directly in code.
Just insert and forget the rest. It will get self adjusted. 😀 Cool! isn’t it.


Targeted Audience – Intermediate.





How to Debug the Release Build?

16 11 2008


“Debugging in release build!!! Is it possible?” I’ve heard this questions a couple of time, especially from Visual C++ Novices. Yes! its possible. But why?
I still remember my first project. We did everything right, debugged and fixed all bugs in debug build and released the application. But client tested the release version and it was like FatMan, a lot of crashes.

So, Debugging in release build is important because, even though the debug build can catch most of issues, there might be some bugs or crashes still hiding under the release version of application. For catching them, there is now way other than to debug the release build itself.

debuginreleasebuild


Well, basically compiler needs debug information for stepping into code while debugging and In release build, by default there will not be any debug information. Compilers cannot interpret or understand optimized code and in release build, optimization will be enabled by default. For debugging in release build, you’ve to generate debug information and to turn off optimizations. Follow the steps about how to tweak project settings for that.

1) Take project settings by Alt+F7.
2) Select Release configuration.
3) Select “C/C++” tab. Set “Optimizations” as “Disable Debug” and “Debug Info” as “Program Database”.

debuginreleasebuild1

4) Take “Link” tab. Enable “Generate Debug Info“.

debuginreleasebuild2

5) Now from menu, take – “Build > Set active configuration…” and select Release build as default.

debuginreleasebuild3

6) Rebuild the project by F7.
7) Now what are you waiting for? Press F5 to debug and enjoy!


Since my first project, I don’t forget how to debug in release version. 😀 and this is my advice for you – “Its fine to do the entire debugging in debug build, because its tuned to catch a lot of bugs. But before releasing check the release build too.”


Targeted Audience – Beginners.





Watch byte streams in Memory Window more easily.

13 11 2008


Assembly programming! It fascinates me always. I love sending packets of data to hardware, parsing back the replies, etc. You might be using BYTE arrays for doing such stuffs. well, in that case, instead of watch window, memory window is preferable to see the byte stream, because it will be so easy to see the entire message.

For instance, assume that we need to send a packet of data in following format.
memorywindow1

Following code snippet generates similar array. Ofcourse in legacy coding style.

// Data packet.
const int DATA_PACKET_LEN = 12;
BYTE DataPacket[DATA_PACKET_LEN] = { 0 };

int Index = 0;

// Add the first DWORD.
DataPacket[Index] = 1;
Index += sizeof( DWORD );

// Add the second WORD.
DataPacket[Index] = 2;
Index += sizeof( WORD );

// Add the third WORD.
DataPacket[Index] = 3;
Index += sizeof( WORD );

// Add the forth DWORD.
DataPacket[Index] = 4;
Index += sizeof( DWORD );

If you watch DataPacket in memory window, it will look like this.
memorywindow2
A chunk of bytes. It seems hard to read and interpret values. isn’t it? More over that, in intel machines, the values are stored in LSB format which makes the readability even more worse. well is there any trick which helps to read the format even better.


If you right click in the Memory window, you’ll get the context menu and in that you can select the preferred byte ordering.
memorywindow3

Select “Long Hex Format” to group bytes as DWORDs. Well, now you can read the byte stream, as such in the packet format picture. cool! isn’t it?
memorywindow4


Yes! i know. You’re thinking about this – the second and third parameter get swapped. Well, its due to the fact that Intel machines follows LSB layout for storing values in memory. This is not perfect solution but it does make life easier and having something is better than nothing! isn’t it? 😉


Targeted Audience – Intermediate.