Recently one of my colleagues and also another user in MSDN forum asked a question related to this. The question is as follows…

If I have a string vector with few elements like “Nibu” “Babu” “Thomas”, how can I get all combinations of these three strings? Results should look like…

“NibuBabuThomas”
“NibuThomasBabu”
“BabuNibuThomas”
“BabuThomasNibu”
“ThomasNibuBabu”
“ThomasBabuNibu”

So in C++ there is a standard C++ algorithm function called next_permutation. Use this function on an array of sorted strings to get required result. So some sample code follows…

typedef std::string VT;
typedef std::vector< VT > VTVec;

// For dumping contents of vector to a stream
void Dump( VTVec& VecToPrint, std::ostream& stream )
{
  // Get iterator for given stream, every element will be separated with second
  // parameter.
  std::ostream_iterator<VTVec::value_type> Itr( stream, ", " );
  // Just one line to dump vector contents, no loops needed
  std::copy( VecToPrint.begin(), VecToPrint.end(), Itr );

  // Remove redundant space and bracket ( ", " ) from the end
  stream << "\b\b  \b\b\n";
}// End Dump

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
  int nRetCode = 0;

  VTVec vecNames;
  vecNames.push_back("Nibu");
  vecNames.push_back("Babu");
  vecNames.push_back("Thomas");

  std::sort(vecNames.begin(), vecNames.end());

  std::cout << "\nNames before applying permutations: ";
  Dump(vecNames, std::cout);
  std::cout << std::endl;

  int Count = 0;
  while(next_permutation(vecNames.begin(), vecNames.end()))
  {
    Dump(vecNames, std::cout);
    ++Count;
  }

  std::cout << "\nFound a total of " << Count << " combinations!\n";
  return nRetCode;
}

Result is as follows…

Share
 

Recently a user asked this question in MSDN forums. He had MBCS enabled for his application and also wanted to enable unicode characters in a particular edit control. Note MFC only create unicode controls if UNICODE is defined in project settings.

So in order to explicitly create controls that support UNICODE characters we use the ‘W’ functions. For example: for our user he should use “CreateWindowW” function. Note the last ‘W’ in the function. ANSI windows are created by calling “CreateWindowA” function (this also means that we can explicitly create a non-unicode control).

Also make sure you use only UNICODE API’s for a control created in this manner. For e.g. always call GetWindowTextW instead of GetWindowTextA. Never use MFC on such a control if it’s not created by MFC framework, I mean don’t call CWnd::Attach on this control. Mainly because MFC is operating in non-unicode environment but our control is a UNICODE one.

Now once such a control is created you can paste characters of different languages, for e.g. Chinese, Japanese, etc even though the application is not a UNICODE supporting one.

Share
 

So after sometime I’m back with a new tip. You know I sometimes forget to adapt copy constructors when I add new members to a class. So at times I end up with strange bugs. So I’ve devised a small utility to remind me to update copy constructors after adding new members to class. Here is the utility…

#ifdef _DEBUG
   #define VERIFY_COPY_CONSTRUCTOR(Class, OldSize)\
   if( sizeof( Class ) != OldSize )\
   {\
      TCHAR szBuf[512] = { 0 };\
      _stprintf_s(szBuf, \
                  sizeof(szBuf)/sizeof(TCHAR), \
                  _T( "Copy constructor not adapted for new members\nOld class size is: %d\nNew class size is: %d\n" ), \
                  OldSize, \
                  sizeof( Employee ));\
      ::MessageBox( ::GetForegroundWindow(), szBuf, _T( "Error!" ), MB_OK|MB_ICONERROR );\
      __asm int 3\
   }
#else
   #define VERIFY_COPY_CONSTRUCTOR(Class, Size)
#endif

class Employee
{
public:
   Employee()
   {
   }

   Employee( const Employee& Emp )
         : m_Name( Emp.m_Name )
   {
      // Actual size as of now is 36.
      VERIFY_COPY_CONSTRUCTOR( Employee, 32 );
   }

private:
   int m_Age;
   std::string m_Name;
};

Note: 32 is given on purpose to trigger an assert.

Now when you run this code an error pops up which says that copy constructor is not updated. Last time when I updated Employee class copy constructor I only added copying code for m_Name but now I’ve got a new member and I forgot to update the copy constructor. So my utility jumps in and gives me this error dialog…


Error Dialog

Error Dialog

 

Note that new size of the class is given in the message box to help you update the hard coded size in the macro. The only thing that you should remember here is to update the hard coded size in the macro (well you’ll that’s for sure ;) ).

PS: There is also something cooler which I didn’t try out yet. VC10 has something called static_assert which will be useful for such purposes. You can try out and let me know ;) .

#ifdef _DEBUG
#define VERIFY_COPY_CONSTRUCTOR(Class, OldSize)\
if( sizeof( Class ) != OldSize )\
{\
TCHAR szBuf[512] = { 0 };\
_stprintf_s(szBuf, sizeof(szBuf)/sizeof(TCHAR), _T( “Copy constructor not adapted for new members\nOld class size is: %d\nNew class size is: %d\n” ), OldSize, sizeof( Employee ));\
::MessageBox( ::GetForegroundWindow(), szBuf, _T( “Error!” ), MB_OK|MB_ICONERROR );\
__asm int 3\
}
#else
#define VERIFY_COPY_CLASS(Class, Size)
#endif

class Employee
{
public:
Employee()
{
}

Employee( const Employee& Emp )
{
VERIFY_COPY_CONSTRUCTOR( Employee, 35 );
}

private:
int m_Age;
std::string m_Name;
};

Share
 

auto keyword is one of those keywords that’s never used. So what the c++ committee has done is, they’ve started using it for a better purpose. For better understanding see this simple demo…

First without using auto keyword…

void AutoTest()
{
  typedef std::vector<int> IntVector;
  IntVector Ints;
  std::generate_n(std::back_inserter( Ints ), 100, rand);

  // After filling some elements into the vector, we iterate through them
  for( IntVector::iterator Itr = Ints.begin(); Itr != Ints.end(); ++Itr )
  {
    // Some code
  }
}

Now let’s try with the auto keyword…

void AutoTest()
{
  typedef std::vector</int><int> IntVector;
  IntVector Ints;
  std::generate_n(std::back_inserter( Ints ), 100, rand);

  // After filling some elements into the vector, we iterator through them
  for( auto Itr = Ints.begin(); Itr != Ints.end(); ++Itr )
  {
    // Some code
  }
}

Hope you noticed the difference, the type for the variable Itr is automagically inferred by the compiler based on the return type from Ints.begin().  Another huge benefit is when using auto keyword to wrap a lamda  expression (an anonymous/inline functor). See this example…

void LamdaTest()
{
   int x = 0;
   auto LamdaFunc = [&x](int y)
   {
     while( y-- > 0 )
     {
       ++x;
     }
   };

   LamdaFunc( 10 );
   LamdaFunc( 100 );

   std::cout < < "Value of x after calling LamdaFunc is: " << x;
}

For us it’s hard to infer or to know how to declare the type of this lamda expression but for the compiler it’s easy (well hope so). When we give auto keyword the type is auto inferred. We then use LamdaFunc object to invoke this lamda expression. In the end x will have the value 110. Note that the expression [&x] means, pass x by reference. I’ll brag about lamda’s in my next post probably.

Cool isn’t it, I liked this feature. 8)

Share
 

Recently a user asked this question in forums, I was quite able to answer his query. So thought this might be useful to you guys too. :)

So the basic difference is that CComPtr is an ATL class is used with using ATL COM implementations while _com_ptr_t is one of the few COM support classes to support non ATL COM development. The definitions of these COM support classes go along with the tlb files and probably that’s why they are called as COM support classes. Also their definition can be found in comdef.h too. The other classes similar to _com_ptr_t are …

  1. _bstr_t
  2. _com_error
  3. _com_ptr_t
  4. _variant_t

So what are COM support classes? These are special lightweight classes that wraps basic useful COM functionality so that ideas like COM smart pointer typedefs over raw COM inteface classes works just by using tlb file. An example on how _com_ptr_t is used…

_COM_SMARTPTR_TYPEDEF(IMyInterface, __uuidof(IMyInterface));

This above line declares _com_ptr_t specialization IMyInterfacePtr.

The library to link to for using COM support classes is comsuppw.lib.

Share
 

Never access an std::string‘s buffer with an intent to increase/decrease it’s length nor pass such a buffer to functions which takes a char*. I did this mistake sometime back and got trapped in a strange bug with operator +=. This is how my code looked.

std::string str( ' ', MAX_PATH );
GetFolderName( pFullPath, &str[0] ); // Oop

Problem with above code is that you won’t get an immediate crash since it’s a properly allocated buffer with MAX_PATH chars. But if you do further operations on such string expect plenty of inconsistencies. This is how my code looked after above piece of code…

str += "\\"; // Append backslash

I was expecting a valid path with backslash appended towards it’s end, but this never happened and debugging with debugger too didn’t help.

So now let me tell you what the exact problem is! When you do &str[0] you pass the address to the first char in std::string‘s buffer. So when function GetFolderName fills in this buffer with folder path the length of std::string is not updated since it’s a C style function and it’s neither expected to do so. So the function terminates given buffer with a ” with std::string‘s length way high (MAX_PATH). So now when I do a += std::string internally fails some condition leading to unexpected results, note that this piece of code never caused a crash but I was quite lucky and watchful enough to fix this stupid bug. Sigh!

So watchout, there is no CString::GetBuffer or CString::GetBufferSetLength type of function for std::string, well at least for now.

Share
 

Are you having trouble with VS2008 after conversion from VS2005 to VS2008? Most common complaints are that the executable is way to slow when compared to it’s counterpart generated with VS2005. The reason for this is given in this MSDN forum thread have a look…

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/fb32033b-7bad-439b-a94c-943a17f0cbb2

The essence of this thread is given below (quote from the thread, thanks to Jon Baggott)…

In Visual Studio 2008 SP1 (SP1 not RTM) there is a serious bug with /O2 optimization. One way this bug can be triggered is by upgrading a project from a previous version. Even though the project setting shows the release build is set to /O2, the build can be not optimized at all. To work around it you have to change the setting to no optimization, apply, and then change it back to /O2. The quick way to see if this is needed is to check whether the setting for optimization is in bold or regular – if’s it’s bold you’re OK; if it’s regular text you’re not. This bug has been reported to Microsoft by many people over the past few months via the Connect feedback site but every case has been closed by them without doing anything and for completely invalid reasons.
Share
 

What’s a breakpoint?

A breakpoint is defined as the location where a debugger breaks execution to allow the user to have a look or to modify the execution context.

What’s new with breakpoints?

With visual studio 2005 and 2008 behavior of breakpoint has changed. Some features that were added are as follows…

  1. Know hit count of a break point, no more need to keep a temp debugger variable for counting hits. For e.g. you can set a conditional breakpoint and then enable hit count. You’ll see how many times the condition was satisfied, you can also disable breaking of execution so that the program keeps running.
  2. Trace local variables/function name and more to the visual studio immediate/output window.
  3. Run a macro when a breakpoint is hit.
  4. We can disable breaking of execution which means there will only be tracing going on and no breaking of execution.

So to access these features, after adding a breakpoint, open the breakpoint window and right click on this breakpoint. Lower half of the dropdown contains these features, here is a screenshot of the context menu…

Breakpoint Context Menu

Breakpoint Context Menu

There are different types of break points available in visual studio. There are four in my knowledge…

  1. File breakpoint – Breaks at a location in a file
  2. Address breakpoint – Breaks at an address
  3. Function breakpoint – Breaks at a function
  4. Data breakpoint – Breaks at specific byte locations

Hit count

Let me show you how to count the number of even numbers from 1 to 100, I know it’s quite easy, it’s fifty, but via debugger? Here is a small function with a loop from 1 to 100.

void CountEven( const int From, const int To )
{
   for( int Index = From; Index < To; ++Index )
   {
      // Set a conditional breakpoint to get the count of odd numbers,
      // Dummy code to allow set a breakpoint
      ::SendMessage( AfxGetMainWnd()->GetSafeHwnd(), WM_NULL, 0, 0 );
   }
}

Now set a break point inside the for loop (Press F9) and right click and select “Breakpoint->Condition” item. You’ll get the following dialog…

Setting a Conditional breakpoint

Setting a Conditional breakpoint

So here I’ve set the condition that whenever Index%2 is zero then we have an even number. So when this happens I’m asking the debugger to break execution. So this works as expected but our aim is to count the number of even numbers using debugger, so for that again right click on the breakpoint line and select “Breakpoint->Hit Count” item, following dialog pops up…

Enable hit count in the debugger

Enable hit count in the debugger

You can see that I’ve selected an option called “break when the hit count is a multiple of”. I’ve given 10 as the option, so that frequency of breaking of execution is less. When I run the code this is what I get in the breakpoints window…

Hit count result

Hit count result

See the “Hit Count” item?  Execution broke 5 times this means for every 10 hits, hence count is 50. Isn’t this fun?

Tracing

Remember the times when we had to trace statements just for debugging purpose, for e.g. we wanted to trace a certain variable’s value when it satisfies a condition. Let’s take the above example, we’ve already enabled a conditional breakpoint such that it’s breaks execution whenever “Index” is an even number. Right click on the breakpoint line and select “Breakpoint” and then “When Hit” option. Following dialog pops up…

When Hit Dialog

When Hit Dialog

Check the first option Print a message”, this is the option that prints a message either to your “immediate window” or to your “Output window” based on the options that you’ve set. Now run the program to have some fun, this is the output in my computer…

Output of "when hit"

Output of "when hit"

All even index values are traced to the output window, isn’t this cool?

Filter

This one is even more cool. It allows us to set breakpoint filters. This is kind of a conditional but with a larger scope. For e.g. if you want to break execution only for a certain thread or only for a certain process. So for this right click on the breakpoint line and select “Breakpionts->Filter”. Following dialog pops up…

Breakpoint filter

Breakpoint filter

Pretty easy to use. So this means for a function that is called from multiple threads you can explicitly tell the debugger for which threads to break execution.

Tid bits

  1. Did you know that you can set a breakpoint in the call stack window by press F9?
  2. Did you know that you can delete all breakpoints by press Ctrl + Shift + F9?
  3. Did you know that you can run to cursor by pressing Ctrl + F10?
  4. Did you know that you display breakpoints window by pressing Alt + F9?
  5. Did you know that you can use “Set Next Statement” by pressing Ctrl + Shift + F10?

Yeap that’s it from me for now, all the best. :)

Share
 

Use stl algorithm random_shuffle. This function has two overloads, first version randomizes using it’s own version of random number generator, second function takes a custom random number generator from our side.

void TestShuffle()
{
    typedef short VT;
    typedef std::vector<vt> VTVector;

    // Count of elements
    const VTVector::size_type Size = 10;

    // Create two vector objects
    VTVector Vect( Size );

    // Generate 10 random numbers
    std::generate( Vect.begin(), Vect.end(), rand );
    // Sort them
    std::sort( Vect.begin(), Vect.end() );
    // Shuffle them to demonstrate shuffle!!!
    std::random_shuffle( Vect.begin(), Vect.end() );
}

//Debugger output after generate call...
//10228,19974,5968,8993,14868,12724,4693,25377,32034,7329
//Debugger output after sort
//4693,5968,7329,8993,10228,12724,14868,19974,25377,32034
//Debugger output after random_shuffle call
//4693,14868,10228,19974,12724,7329,32034,8993,25377,5968

Share
 

Never use a temporary variable to swap two stl containers instead always use std::swap function. This function has different overloaded versions specialized in swapping every standard container.

So what’s the difference if we use a temporary variable swap and std::swap. The idea is that basically all stl containers use dynamic memory to store data, so for swapping such two containers we don’t have to copy every item in such a container but just to swap few internal pointers, since both containers are of the same type.

Here is a small function and it’s output which swaps two vectors containing plenty of elements. Take a look at the output…

void TestSwap()
{
    typedef double VT;
    typedef std::vector<vt> VTVector;

    // Count of elements
    const VTVector::size_type Size = 1000000;

    // Create two vector objects
    VTVector Vect1;
    VTVector Vect2;

    // Fill both vectors with random elements
    std::generate_n( std::back_inserter( Vect1 ), Size, rand );
    std::generate_n( std::back_inserter( Vect2 ), Size, rand );

    // Now swap and test performance //

    // Performance measurement class
    Performance Perf;

    // No scientific notation needed and also increase precision
    std::cout < < std::fixed << std::setprecision( 16 );

    // Using temporary variable
    {Perf.Start();
    VTVector VTTemp = Vect1;
    Vect1 = Vect2;
    Vect2 = VTTemp;
    const double PerfVal = Perf.Stop();
    std::cout << "Swap performance with temporary variable: " << PerfVal << " ms";}

    // Using std::swap function
    {Perf.Start();
    std::swap( Vect1, Vect2 );
    const double PerfVal = Perf.Stop();
    std::cout << "\nSwap performance with std::swap function: " << PerfVal << " ms\n";}
}

Output:
Swap performance with temporary variable: 0.0191267064952015 ms
Swap performance with std::swap function: 0.0000026732372498 ms

So the output settles all debates. :) Performance measurment class sourcecode is available here…

Share
© 2012 bits and bytes Suffusion theme by Sayontan Sinha