As far as I know, dynamic memory allocation can be done in C by using the malloc
function :
int *p = (int *)malloc(sizeof(int))
My question is :
How does dynamic memory allocation work, and how is it different from the normal method i.e. static allocation in terms of working ?
What is the role of the statement (int *)
before the malloc()
function ?
What role does type casting play here? (if it does that is )
How does dynamic memory allocation work and how is it different from the normal method in terms of working ?
You have library that manages pages of memory allocated from the operating system at run time. The library manages those pages and subdivides them to handle the generally smaller requests made by malloc() calls.
In contrast, static variables are allocated by the linker and loader. The linker defines the layout of the program in memory and creates an executable file containing instructions for the loaders to set the program's initial state.
what is the role of the statement (int *) before the malloc() function ?
Eliminates warning messages that you might get from implicit (void*) to (int*) conversion
What role does type casting play here ? (if it does that is )
See above.