Category Archives: Programming

Heap Segmentation Fault – caused by declaration location/scope

Unexpected segmentation faults? Might be your variable declaration.

Here’s the point, declaring variables as global, is commonly known as “do you really need to?” .
Especially if it’s a dynamic sized data structure, like a C++ list.
Yeah  I realized the hard way, the size grew out of static memory bounds and got into the heap.

Good old GDB to the rescue, those error messages might be a mystery at first, it’s still potentially a beacon of hope.


Learning to use Vim

Awesome links


Dynamic Multidimensional Array in C and C++

Here’s how you would declare a one dimensional array in C:

 long* array;
 array = malloc(dimension*sizeof(long));

Equivalent to:
long array[dimension];

For two dimensional array in C, where each element are located in contiguous memory space:

long** array2;
long* tmp;
array2 = (long**)malloc(dimension1 * sizeof(long*));
 tmp = (long*)malloc(dimension1 * dimension2 * sizeof(long));
  for (i = 0;i<dimension1;i++){
	array2[i] = tmp + (i*dimension2);

One dimensional array in C++:

int *array;
array = new int [dimension];

For two dimensional array in C++:

int **array2;
array2 = new int* [dimension1];
for (i=0; i<dimension1; ++i)
   array2[i] = new int[dimension2];

If you get segmentation faults, and you have arrays in your program, most likely it’s because of those pointers. And never try to access a pointer that leads to NULL. Fault!

Error on Linking MSVC++ Projects

Upon successful compilation of builds, the linking process  might stumble upon a linking error. These may happen in the form of unresolved externals or redefinitions.

On unresolved externals: make sure all of the dll or lib exists. Also check the code generation property and select the appropriate options. Make sure all projects use the same code generation correspondingly.

On redefinitions, I had to ignore one of the libraries which caused the redefinition. For instance, in my project, I had to ignore these libraries for the release build :libcmtd.lib;libc.lib;nafxcw.lib;msvcrtd.lib;msvcrt.lib

And these for debug build:

Would be grateful for enlightenments on further details of those libraries. I could’ve saved hours on solving the linking errors.

PS: Don’t forget to set EMBED MANIFEST to yes when using non DLL settings. Found this out the hard way.

Macros for Build Commands in MS Visual Studio

Other than the ones stated here:

We can also use Windows System Environment Variables (under Control Panel->System Properties) to retrieve the path. For instance: ($DXSDK_DIR)
will lead to the DirectX SDK directory, includes a trailing backslash.
I needed to use these macros since the software development are conducted on different machines with different SDK directories/drive letters. Absolute include directories should always be avoided. 🙂

Internet Explorer Script Error MFC (Line 815 Char 3)

I am using Visual Studio 2005 SP1 (C++), and I wanted to add a class for a dialog when the IE error showed up. Needed to find a quick fix and here is what I found: (turns out it was because I installed IE8)

Please follow these steps:

–          Open regedit (on a 64-bit OS, open the 32-bit regedit)

–          Under “HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet
Settings\Zones”, create a new key called 1000 (if it isn’t already there)

–          Under 1000, create a DWORD entry with:

o   Name = 1207

o   Type = REG_DWORD

o   Data = 0x000000