Memory Management
Memory Management
Memory
• A memory or store is required in a computer to store
programs (or information or data).
• Data used by the variables in a program is also loaded
into memory for fast access.
• A memory is made up of a large number of cells, where
each cell is capable of storing one bit.
• The cells may be organized as a set of addressable
words, each word storing a sequence of bits
• These addressable memory cells should be managed
effectively to increase its utilization.
2.1. MEMORY ALLOCATION IN C
• There are two types of memory allocations in C:
• 1. Static memory allocation or Compile time
• 2. Dynamic memory allocation or Run time
• In static or compile time memory allocations, the
required memory is allocated to the variables at the
beginning of the program.
• Here the memory to be allocated is fixed and is
determined by the compiler at the compile time itself.
• For example,
• int i, j; //Two bytes per (total 2) integer variables
• float a[5], f; //Four bytes per (total 6) floating point variables
2.1. MEMORY ALLOCATION IN C
• When the first statement is compiled, two bytes for both
the variable ‘i’ and ‘j’ will be allocated.
• Second statement will allocate 20 bytes to the array A
[5 elements of floating point type, i.e., 5 × 4] and four
bytes for the variable ‘f ’
Drawbacks static memory allocation
It is also possible that the memory allocated is much larger than necessary,
i.e., we want to reduce the memory space.
In both the cases we want to change the size of the allocated memory block
and this can be done by realloc() function.
This process is called reallocation of the memory. The syntax of this function is
• ptr = realloc(ptr, New_Size)
Where ‘ptr’ is a pointer holding the starting address of the allocated memory
block.
And New_Size is the size in bytes that the system is going to reallocate.