In C, the malloc() function allocates memory dynamically on the heap. The statement 'char *ptr = malloc(10);' allocates 10 bytes on the heap and stores the address in pointer ptr (which itself is on the stack). The stack is used for local variables, code segment for instructions, and data segment for static/global variables.