hi,
im learning the c programming language .
i didn't understand the portion memmory allocation (malloc ,calloc ).
please tell me how do we allocate the memmory using the malloc and calloc ?
It should also be mentioned when allocating memory with malloc(), calloc() or realloc(), it should also be deallocated when no longer utilized, using free(). These memory allocations are persistent until explicitly freed by free() or program termination.
Failing to deallocate memory can result in what is referred to as a "memory leak." Garbage collection is not automatic in either C or C++.
this functions allocates the required memory space in the heap specified by the argument
e.g: int *array = (int*)malloc(sizeof(int)*10);
the argument to malloc() is the amount of memory. it is advisable to specify using sizeof() operator so that the code is ported to other architectures
array is pointer to series of memory locations, with a step increment of size of integer data type.
the data type returned by malloc() is void* so we've to parse it according to our requirement.
dont forget to deallocate the memoty allocated by malloc() or calloc() using free(); else u'll end up in memory leak.