C Memory Management

Memory management is one of the most fundamental and important aspect for any computer programming language. In many programming language you don’t have to worry about the memory management, but in C Programming Language you to do memory management manually. C is one of the most powerful programming language. Pointer is one of the most powerful feature of the C programming language which gives you the ability to point memory locations. This feature enables you easy memory access.

As we all know computer memory is made up of large number of cells, which further organized as addressable registers. Each register is responsible to store a sequence of bits.So it is always necessary to manage these memory locations effectively to increase the utilization.

Types of memory allocations in C

There are two ways in which memory can be allocated in C

  • Static memory allocation or Compile time allocation
  • Dynamic memory allocation or Run time allocation

Static or compile time memory allocation

In this method desired memory is allocated at the time of variable declaration or beginning of the program. Amount of memory allocation is fixed and is determined at the time of program compilation.

Example:

when the first statement int a,b; is compiled 4 consecutive bytes(integer occupies 4 bytes in a 32 bit machine) for each variable a,b will be allocated.

Drawbacks of static memory allocation

  • If we declare more number of elements and we are using less number of elements then the memory allocated for the not used variables will be wasted.The unused memory cells are not made available to other applications.This leads to inefficient use of memory.
  • Size for the data type or variable must be know before.
  • We can not change the size of variable at run time.

Dynamic or Runtime memory allocation

In C Programming Language it is not always possible to predict the memory requirement for the program, so it is better to allocate the memory in real time when ever required, it makes efficient use of memory.

C Programming Language provides the following dynamic allocation and de allocation function –

  • malloc()
  • calloc()
  • realloc()
  • free()

malloc,calloc and realloc is used for memory allocation and delocation is achieved by the use of free function.

Allocating A Block of Memory-malloc()

The malloc() function allocates the memory at run time whenever required. It allocates the memory block with required number of contiguous bytes in the memory and returns the pointer to the first byte(base address) or null if there is some error occur.

Syntax:

Example:

  1. First we declare a pointer variable ‘p’.
  2. Then the pointer itself is equal to a pointer type int that contains the memory address space for an int.
  3. Sizeof () return the space it occupies what you want, if you put int in, such as 2 bytes, because we have assigned two bytes.
  4. Finally, now that the pointer is contained, we give a value.

Allocating Multiple blocks of Memory-calloc()

The calloc() function is same malloc() except it is used to allocate multiple memory block of same size then all bytes sets to zero.

Syntax:

It allocates contiguous space for ‘n’ blocks,each of size of block_size bytes.All bytes are initialized to zero and a pointer to the first byte of the allocated memory block is returned.

Example:

An int array of 10 elements can be allocated as follows.

We can achieve same using malloc() function as below –

However area reserved by malloc() function to the states that are undefined, the area allocated by the calloc function contains a 0.

Differnce between malloc() and calloc()

  1. malloc() allocates a bytes of memory but calloc() allocates blocks of memory.
  2. malloc() takes a single argument(memory required in bytes) but calloc() takes 2 arguments (number of variables to allocate memory,size in bytes of a single variable)
  3. The memory allocated using malloc() function contains garbage values,the memory allocated by calloc() function contains the value 0.

Resize the size of a Memory-realloc()

The realloc() function alllow us to reset the size of the allocated area.

Syntax:

Where ptr is holding the current base address of the memory block and new_size holds the size in bytes.

Releasing the used memory-free()

The free() function is used to de allocate the allocated memory using malloc() or calloc() function. It is always important to release the memory that is not in use, so it can be used in future.

Syntax:

Note :- ptr must be a valid pointer to the address otherwise it can result into system crash.

Example Program:

Below is the program where we have allocated memory for ptr integer pointer with the byte required for int data type, then assigned value 10. Finally released memory using free() function.