In C, the region of memory that is available at run time is called the heap. In C++, the region of available memory is known as the free store. The difference between the two lies in the functions you use to access this memory.
To request memory from the heap in C, you use the malloc function. For instance, you can dynamically allocate a date structure as follows:
struct date *dateptr;
dateptr = (struct date *)malloc( sizeof( struct date ) );
The malloc function allocates a block of memory large enough to hold a date structure, and returns a pointer to it. The malloc function returns a void pointer, which you must cast to the appropriate type when you assign it to dateptr. You can now treat that block of memory as a date structure.
In C++, however, malloc is not appropriate for dynamically allocating a new instance of the Date class, because Date's constructor is supposed to be called whenever a new object is created. If you used malloc to create a new Date object, you would have a pointer to an uninitialized block of memory.You could then call member functions for an improperly constructed object, which would probably produce erroneous results. For example:
Date *datePtr;
int i;
datePtr = (Date *)malloc( sizeof( Date ) );
i = datePtr->getMonth(); // Returns undefined month value
If you use malloc to allocate objects, you lose the safety benefits that constructors provide. A better technique is to use the new operator.