How To Find Memory Leaks With The Debugger

September 24, 2008

Visual studio debugger is a wonderful invention. See how it can show memory leaks (memory not properly de allocated) on exiting a program.

#include "afx.h"

// To show the memory leaks on exit
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

int main()
   char* str = new char[10];  // This won't be deallocated
   return 0;

See the following screen shot.

If DEBUG_NEW (defined in afx.h) is used, memory will be allocated in on a memory tracking basis (in the debug version).

You have to declare the above block in each file you need memory tracking.

Some Background Info

VC++ debugger defines some magic values for each type of memory location allocated.

0XCDCDCDCD – Memory allocated in heap, but not initialized

0xDDDDDDDD – Heap memory released

0xFDFDFDFD – Specifies the boundary of heap memory. It should not be overwritten as it designated the end.

0XCCCCCCCC – Memory allocated in stack, but not initialized.

How To Hard Code a Break Point

September 23, 2008

I’m sure you are familiar with debug break points (press F9 in Visual Studio and a break point is there for you). What about programatically putting a break point? Try the following.

__asm int 3;

How To Show Compilation Duration in Visual Studio

September 22, 2008

Just add /Y3 in the shortcut command line of VC++ 6.

“C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin\MSDEV.EXE” /y3

The compiler will show how much time it took to compile the whole project.

In Visual Studio .NET, you can set this option under Options –> Projects –> Build.

How To Get The Size Of Memory Allocated In Heap

September 20, 2008

sizeof() function can not give the size of memory locations allocated using new, malloc, calloc and realloc. VC++ provides a macro for the above purpose and it is _msize. See the following code segment.

using namespace std;
int main()
   // Allocate 10 integer locations 
   // = a toatl of 40 in 32 bit platforms
   int* pInt = new int[10];
   size_t Size = _msize(pInt);
   cout << Size << '\n';       return 0; } [/sourcecode] The result will be 40.

How To Sort A Vector With Non Numeric Elements

September 19, 2008

Have you ever faced a problem to sort STL vector with non number elemnts (say structs or other classes)? The problem with sort function is that it should know about the less than (<) operator. So the problem would be solved if you defined a < operator for the element type.

The following example illustrates how to sort a vector with elements of type struct.

using namespace std;

// Here is a simple struct
struct MyStruct
   int Num;
   // Define the operator <    bool operator <(const MyStruct& Rhs)    {       return (Num < Rhs.Num);          } };   int main() {      vector MyVector;
   // Let the size be 5.
   // Push 5 instances of MyStruct with Num ranging
   // from 5 to 1
   MyStruct TestStruct;
   int i = 0;
   for (i = 0; i < 5; ++i)    {       TestStruct.Num = 5 - i;       MyVector[i] = TestStruct;    }    // Now sort the vector    sort(MyVector.begin(), MyVector.end());    // Try to display Num for each element. It is sorted    for (i = 0; i < 5; ++i)    {       cout << MyVector[i].Num << '\n';    }    return 0; } [/sourcecode] You can use the same technic for any vector with non number elements. The only condition is that you should have a < operator defined for the element data type.

How To Sort An Array

September 18, 2008

The same sort function used to sort STL vector can be used for sorting arrays also. Surprised? Sometimes life is easier than we expect. 

using namespace std;

int main()
   int MyArray[] = {10, 9, 8, 7, 6};
   sort(MyArray, MyArray + 5);
   for (int i = 0; i < 5; ++i)    {       cout << MyArray[i] <<'\n';    }    return 0; } [/sourcecode]

How To Convert CString To TCHAR*

September 18, 2008

See the following code segment.

CString myCString = _T("Hello"); 
const TCHAR* myCharString = LPCTSTR(myCString);

Some Background Info

  1. TCHAR will be evaluated to char if _UNICODE is not defined. Otherwise it will be evaluated to wchar.
  2. LPCTSTR is also a data type which is nothing but const char* or const wchar*, depending on whether _UNICODE defined or not.
  3. CString has a constructor which accepts TCHAR*.
  4. The macro _T will make the supplied string neutral. That is, if _UNICODE is defined, the string will be in 16 bit unicode format, otherwise in 8 bit ANSI format.