CHAPTER 19

image

Memory Management

In the examples so far, the programs have only had as much memory available as has been declared for the variables at compile time. This is referred to as static allocation. If any additional memory is needed at runtime it becomes necessary to use dynamic allocation. The C standard library provides several functions for managing dynamically allocated memory, including: malloc, free, and realloc. These functions are found in the stdlib.h header file.

Malloc

The malloc function takes a size in bytes and returns a pointer to a block of free memory of that size. This dynamically allocated memory is uninitialized and can ...

Get C Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.