Programming in C: Unit III (b): Pointers

Memory Usage

Programming in C

Before jumping into dynamic memory allocation, let us first understand how memory is used. Conceptually, memory is divided into two-program memory and data memory

MEMORY USAGE

Before jumping into dynamic memory allocation, let us first understand how memory is used. Conceptually, memory is divided into two-program memory and data memory (Figure 7.10).

The program memory consists of memory used for the main () and other called functions in the program, whereas data memory consists of memory needed for permanent definitions such as global data, local data, constants, and dynamic memory data. The way in which c handles the memory requirements is a function of the operating system and the compiler.

When a program is being executed, its main () and all other functions are always kept in the memory. However, the local variables of the function are available in the memory only when they are active. When we studied recursive functions, we have seen that the system stack is used to store a single copy of the function and multiple copies of the local variables.

Apart from the stack, we also have a memory allocation known as heap. Heap memory is unused memory allocated to the program and available to be assigned during its execution. When we dynamically allocate memory for variables, heap acts as a memory pool from which memory is allocated to those variables.

However, this is just a conceptual view of memory and implementation of the memory is entirely in the hands of system designers.

Programming in C: Unit III (b): Pointers : Tag: : Programming in C - Memory Usage