Construct C++ objects at predefined Memory Locations.

1 04 2008

Icon Description
Usually we use new operator to allocate objects. The new operator will allocate memory for object and calls the object’s constructor. We can construct our object in predefined memory location by using Placement new.

Icon How Can I Do It?
In placement new, you can pass the preferred memory location to the new operator at which the object to be constructed. During placement new, the new operator just calls the constructor. Responsibility of memory allocation is ours. After usage we should call the destructor manually to delete the object. Please see the following code block about usage of placement new.

// Allocate memory ourself
char* pMemory = new char[ sizeof(MyClass)];

// Construct the object ourself
MyClass* pMyClass = new( pMemory ) MyClass();

// TODO: Use pMyClass and enjoy!!!

// The destruction of object is our duty.

Icon Note
The advantages of placement new are as follows –
1) Can allocate a pool of memory and construct objects on our wish.
2) This prevents memory allocation failure fear in runtime, because pool of memory is already allocated.
3) Object creation will be a bit faster, because memory is already allocated and only the constructor is to be called.
4) Since you can place your objects at your preferred memory locations, its very useful in embedded programming. For instance the hardware may be placing 255 bytes of data at memory location – 0x1000. In that case, write a class with an array of 255 Bytes as member variable and construct the object at 0x1000. Now you can access the data by using that object.

And one more interesting bit of information – The placement new is the only scenario in which we are legally allowed to call destructor explicitly as per C++ standards. The “New” New informations are interesting… isn’t it? 😉

Targeted Audience – Intermediate.

How to make your debug build – a slim beauty.

1 04 2008

Icon Description
Usually debug builds are bulky. It will be nice, if we can reduce the size of debug binaries before delivery.

Icon How Can I Do It?
If you turn off the “Incremental Linking” option, the size of debug build will be reduced dramatically. But beware, it can increase your total build time.

1) Take project settings.
2) Go to Link tab
3) Disable “Link Incrementally”

Now rebuild and check the size. Wow!!! She became really slim. 😉

Icon Note
1) You have to change the “Settings->C/C++->Debug Info” to “Program Database”. Or else you’ll get a warning.
2) Turning off Incremental Linking can increase your build time. So its recommenced only before delivery.

Targeted Audience – Beginners.

Integers with fixed size.

1 04 2008

Icon Description
While manipulating integer, in some cases we are worried about the size of int. Because, it depends the word length of processor. For instance while generating a binary message for your hardware etc.

Of course you have some macros such as BYTE etc which ensures the size in all processors word lengths, but will you get the real feel of a built-in integer?

Icon How Can I Do It?
Microsoft had provided __intn data types for integer values with different lengths. They are,

// Directly from MSDN.
__int8 nSmall;      // Declares 8-bit integer
__int16 nMedium;    // Declares 16-bit integer
__int32 nLarge;     // Declares 32-bit integer
__int64 nHuge;      // Declares 64-bit integer

You can use them without worrying about the length.

Targeted Audience – Beginners.