Tuesday, August 3, 2010

Common Pitfall : While using malloc instead of new.

Two mostly used things that help in allocating memory are malloc and new.

Sometimes knowledge of using malloc, for allocating memory in common and less complicated situations, can prove wrong in complicated situations.

Situation [1] (Complication Level 1):
int *pData = (int*)malloc(10 * sizeof(int));
Now to access integers at index from 0 to 9 simply use pData[1] or *(pData+2)...
Very simple!!!

Situation [2] (Complication Level 2):
struct ABC
int P1;
char P2;
ABC *pData = (ABC*)malloc(10 * sizeof(ABC));

Now to access integer P1 of say 4th struct ABC : pData[3].P1
Again Very simple!!!

Situation [3] (Complication Level 3):
struct ABC
int P1;
char P2;

struct PQR
int P1;
char P2;
PQR *pData = (PQR*)malloc(10 * sizeof(PQR));

Now to access integer P1 of ABC which is inside of say 5th struct PQR : pData[4].P3.P1
Again Moderately simple!!!

In above 3 situations memory was allocated and then utilized according to the structure\ layout of structs.

Situation [4] (Complication Level 4):
struct XYZ
int P1;
char P2;
CList P3;
XYZ *pData = (XYZ*)malloc(10 * sizeof(XYZ));

Now try to insert elements in the list P3 of say 4th struct XYZ:
Complie Status OK.
Run Status CRASH!!!
Without reading further think a minute why the code did not worked even though memory was already allocated?

This happened because though memory was allocated for all the data members of list XYZ::P3 but those data members were not initialized with default values.

struct XYZ in above example is composite data type (a class or user defined data type) in which the default value initialization of its data-members is normally done in constructor of the class.

In a composite data type the data-members are created (here created means calling of their respective constructor) prior to the invocation of composite data type constructor itself. ("Refer how Object creation takes place")

So what will initialize the data-members along with the task of allocating the memory for members.
Answer : operator new

XYZ *pData = new XYZ[10];
Here 'new' invokes the constructor of both, the data members of struct XYZ and the struct XYZ itself. Inside the CList constructor, members can be initialized with default values.

[1]Anyway, in C++ new is the preferred mechanism for memory allocation.
[2]Also try to minimize mixing of malloc and new.

Monday, July 19, 2010

Second technical article.

While working with functions, I found a common pattern. A pattern in which all the parameters constituting the parameter-list were bundled in a struct and that struct was passed as a parameter to the function. Therefore, gave a thought on this, in order to find what can be the pros and cons of this pattern. Outcome was the following article:

Description: An article highlighting advantage of using composite data-type for passing parameters to functions.
Parameter Passing :- by Train vs by Truck.

Wednesday, July 14, 2010

C\ C++ Pointer confusion.

Working with pointers by passing them here and there and not getting expected result!!!
Don't go here and there but go directly to :
Pointer Alises.

Tuesday, July 13, 2010

First technical article.

Check out my first technical article on codeproject.com.
Description: An article on creating a framework for command processing using Command Design Pattern.
Converting a class into a CommandHandler.

Hello World

Yes, most of the aspiring programmers starts their programming journey by displaying "Hello World". As simple as that. Today I created my blog and just want to say Hello World. Yes, you guessed right!. I also want to start hitting the keyboard...