Dynamic memory allocation

Almost every application needs to allocate memory dynamically. The data segment of an application does NOT grow. All variables there have been allocated at compiletime and the sizes are fixed. The area where the application gets dynamic memory is called heap. The heap starts at the end of the data segment and grows against the bottom of the stack. If both meet your program is in trouble and will be terminated by the operating system.

Note

Allocation of dynamic memory is THE area of application programming with the largest potential for bugs or very slow performance. At least the performance problem does also exist in languages with garbage collection.

Here is how dynamic memory allocation in C works: void *malloc(size_t size) allocates memory. Please check the return value for not being "null". A null indicates that the system is out of memory. And void free(void *ptr) gives the memory back to the pool.