How to define alignment for structure member variables

25 01 2018

Every data type has an alignment associated with it which is mandated by the processor architecture rather than the language itself. Aligning data elements allows the processor to fetch data from memory in an efficient manner and thereby improves performance. The compiler tries to maintain these alignments for data elements to provide optimal performance. How can we change it?

Windows provide pack directive to set the alignment for structure, union, and class members.
1. #pragma pack(n) – Sets the new alignment.
2. #pragma pack(push)- Pushes the current alignment setting on an internal stack
3. #pragma pack(pop)- Restores the alignment setting to the one saved at the top of the internal stack

struct Struct1
  char a;
  int b;
  char c;

//pushes the current alignment setting on an internal stack
#pragma pack(push) 
#pragma pack(1) //sets the alignment 
struct Struct2
  char a;
  int b;
  char c;
#pragma pack(pop) //restores the alignment

struct Struct3
  char a;
  int b;
  char c;

#pragma pack(push) //pushes the current alignment 
#pragma pack(2) //sets the alignment 
struct Struct4
  char a;
  int b;
  char c;
#pragma pack(pop) //restores the alignment

int _tmain(int argc, _TCHAR* argv[])
  /*| 1 | 2 | 3 | 4 |

    | a(1) | pad.................. |
    | b(1) | b(2) | b(3) | b(4)    |
    | c(1) | pad.................. | */
  int nSizeWithDefaultPadding = sizeof(Struct1); //12

  /*| 1 |

    | a(1) |
    | b(1) | 
    | b(2) | 
    | b(3) | 
    | b(4) |
    | c(1) |  */
  int nSizeWithPadding1 = sizeof(Struct2);//6
  int nSizeAfterRestored = sizeof(Struct3 );//12

  /*| 1  | 2 |

  | a(1) | pad  |
  | b(1) | b(2) |  
  | b(3) | b(4) |
  | c(1) | pad  |  */
  int nSizeWithPadding2 = sizeof(Struct4);//8
	return 0;

In Visual Studio 2015 and later we can use the standard alignas and alignof operators for same.

How to increase the tooltip display time

21 01 2018

The CToolTipCtrl class provides the functionality of the Windows common tool tip control, a small pop-up window that displays the text describing the purpose of a tool in an application. Sometime you may need to increase the display duration of the tool tip if it contains a lengthy text.

CToolTipCtrl provides SetDelayTime function to sets the initial, pop-up, and reshow durations for a tool tip control.

void SetDelayTime(DWORD dwDuration, int iTime);

dwDuration – DWORD that specifies the type of duration that you want to set.
iTime – Integer that specifies the length of the delay time, in milliseconds.

The type of duration for increasing the tool tip display time is TTDT_AUTOPOP.

CToolTipCtrl m_tooltip;
m_tooltip.SetDelayTime (TTDT_AUTOPOP, 10000); //Set tooltip display time to 10s

The default display duation is based on the double-click time of mouse, which can be calculated using windows API GetDoubleClickTime as shown below:

GetDoubleClickTime() * 10;

A Happy New Year to all my blog readers!

2 01 2018


How to debug a function in release mode

29 07 2015

The Visual Studio IDE provides different optimization options that help us to create code for maximum speed or minimum size. Mostly we select /O1 optimizes code for minimum size when we build our project in release configuration. But you might be observed that the variable values may not be displayed correctly in the watch window if we try to debug our source code in release configuration. Of-course, we have options to enable debugging support in release mode. But just think in case if you want to debug just one function in release mode!!!

Pragma directives specify machine- or operating-specific compiler features. The compiler optimization can be performed on a function-by-function basis using #prgma optimize statement.

#pragma optimize( "[optimization-list]", {on | off} )

We can disable the optimization using the optimize pragma with the empty string (“”).

#pragma optimize( "", off )
void FunctionToDebug()
#pragma optimize( "", on ) 

Some pragmas provide the same functionality as compiler options. When a pragma is encountered in source code, it overrides the behavior specified by the compiler option.

How to check whether a memory address is valid

29 07 2015

As a programmer, some time we may need to check whether the specified memory block is within our application’s memory space. How can we do it?

Windows provides an API AfxIsValidAddress for same.

BOOL AfxIsValidAddress(const void* lp, UINT nBytes, BOOL bReadWrite = TRUE);

lp : Points to the memory address to be tested.
nBytes : Contains the number of bytes of memory to be tested.
bReadWrite : Specifies whether the memory is both for reading and writing (TRUE) or just reading (FALSE).

Return Value
AfxIsValidAddress() returns TRUE if the passed parameter points to at least nBytes of accessible memory. If bReadWrite is TRUE, the memory must be writable; if bReadWrite is FALSE, the memory may be const.

How to enable Memory Leak Detection

7 03 2014

Memory leak is defined as the failure to correctly deallocate the memory that was previously allocated and it is a hard to detect bug in C++ applications. The program that leaks large amount of memory or leak progressively may decrease the performance of your system. These kinds of memory leaks make unexpected crashes in our applications and the most difficult part is to reproduce the defect and find out the root cause of same. So it is always better to analyze the memory leaks together with the functionality testing of our application. Lots of 3rd party tools are available for detecting memory leaks in our C++ code. AQTime is one of the best tools I ever used.But in this post I would like to discuss about enabling memory leak detection using Windows APIs.

The visual studio debugger along with C Run-Time Libraries (CRT) can help us to detect memory leaks in our code.

To enable the debug heap functions, include the following statements in your program:

#include "stdlib.h"
#include "crtdbg.h"

you can place a call to _CrtDumpMemoryLeaks before an application exit point to display a memory-leak report when your application exits:


This displays memory leak information in the Output window of Visual Studio IDE.

The #include statements must be in the order shown above. Otherwise, the functions you use may not work correctly.

How to find all files from a folder

29 10 2013

Sometimes we may need to find out all files belongs to a folder. How we will do it?

The FindFirstFile function opens a search handle and returns information about the first file with a name that matches the specified pattern. After the search handle is established, use the FindNextFile function to search for other files that match the same pattern.

void FindFilesFromFolder(CString p_strFolderPath, CStringArray* p_pFiles)
    WIN32_FIND_DATA   FindFileData;
    CString strTemp;

    strTemp.Format(_T("%s\\%s"), p_strFolderPath, _T("\\*.*"));
    hFile = FindFirstFile(strTemp, &FindFileData);
    CString strFilePath;

    if (INVALID_HANDLE_VALUE != hFile) 
            //Skip directories
            if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)

            strFilePath.Format(_T("%s\\%s"), p_strFolderPath, FindFileData.cFileName);
        } while(FindNextFile(hFile, &FindFileData));


Please give the exact file extension incase if you need to find out only files which has specific extension.