Dynamic memory allocations refers to the method of allocating a block of memory and releasing it when the memory is not required at the time of running the program. A block of memory can be used to store values of simple or subscripted variables and a block of memory can be accessed using a pointer. Following are the functions used in dynamic memory allocation, and these functions are available in the header file <alloc.h> ( "alloc.h is a non-standard header file" ) .
|
malloc() function is used to allocate a single block memory to store values of specific data type. It also assigns the address of the first byte of the allotted space to a pointer.
ptr = (type *)malloc(size);
calloc() function is used to allocate memory in multiple block of same size during the program execution. The space alloted is used to store values of an array or structure.
ptr = (type *)calloc(n, m);
realloc() function is used to modify or re allocated the memory space which is previously allotted.
ptr = realloc(ptr, size);
free() function is used to release the memory space which is allotted using malloc(), calloc() or realloc() function.
free(ptr);
A dangling pointer is a pointer that does not point to a valid object and consequently may make a program crash or behave oddly. In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.
It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.