Special Notes To: Allocating Memory Storage
Both C and C++, allow dynamically allocate and
free-up memories from a region known as
"Free-Store" or "HEAP". The pointer ( * ) of C/C++
language, is deeply involved in allocating memories from the heap.
It introduces a variable, pointee, to the compiler. The
complier then engages the functions, (like malloc() or calloc() ) of
C language or the object, "new" of C++ language, to carry out the
job.
int x; /* global stack storage */
int main(void) {
int y; /* local stack storage */
char ch1; /* local stack storage */
ch1 = malloc(50); /* allocates 50 bytes of dynamic heap storage */
size = calcSize(10); /* dynamic heap storage */
}
A pointer is used access the location
(address) where data is stored during the life cycle of the current
application.
As a rule of thumb, you need to free
the space allocated with malloc() or new. Failing to free-up the
allocated memories can cause a slow down or crash your system, this
cause is coined as "memory leak".
The purpose of malloc()
function, is to find a free space of memory on the heap, return an
address so that you allocate the block of
data, and free up the allocated memory with another function,
"free()", as soon as the task is
completed.