<P> These limitations are avoided by using dynamic memory allocation in which memory is more explicitly (but more flexibly) managed, typically, by allocating it from the free store (informally called the "heap"), an area of memory structured for this purpose . In C, the library function malloc is used to allocate a block of memory on the heap . The program accesses this block of memory via a pointer that malloc returns . When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes . </P> <P> Some platforms provide library calls which allow run - time dynamic allocation from the C stack rather than the heap (e.g. alloca ()). This memory is automatically freed when the calling function ends . </P> <P> The C dynamic memory allocation functions are defined in stdlib. h header (cstdlib header in C++). </P> <Table> <Tr> <Th> Function </Th> <Th> Description </Th> </Tr> <Tr> <Td> malloc </Td> <Td> allocates the specified number of bytes </Td> </Tr> <Tr> <Td> realloc </Td> <Td> increases or decreases the size of the specified block of memory . Reallocates it if needed </Td> </Tr> <Tr> <Td> calloc </Td> <Td> allocates the specified number of bytes and initializes them to zero </Td> </Tr> <Tr> <Td> free </Td> <Td> releases the specified block of memory back to the system </Td> </Tr> </Table>

When do you need to allocate memory in c