Static & Dynamic Memory Allocation

Static Memory Allocation:
  • In Static Memory Allocation the memory for your data is allocated when the program starts.
  • The size is fixed when the program is created.
  • It applies to global variables, file scope variables, and variables qualified with static defined inside functions.
  • This memory allocation is fixed and cannot be changed, i.e. increased or decreased after allocation. So, exact memory requirements must be known in advance.

Example:
                                int main()
                                 {
                                    int a;
                                    char str[20];
                                }

Advantages:
  • Simplicity of usage.
  • Efficient execution time.
  • Need not worry about memory allocation/re-allocation/freeing of memory
  • Variables remain permanently allocated.
Disadvantages:

  • Main disadvantage is wastage of memory.
  • Memory can't be freed when it is no longer needed.


Dynamic Memory Allocation:

  • The process of allocating memory at runtime is known as dynamic memory allocation.
  • Library routines known as memory management functions or operators are used for allocating and freeing memory during execution of a program.
  • These functions are defined in stdlib.h header file.

Example: Sometimes the size of the array you declared may be insufficient. To solve this issue, you can allocate memory manually during run-time. This is known as dynamic memory allocation in C programming.

Dynamic memory allocation in c language is possible by 4 functions:

malloc()

calloc()

realloc()

free()

These functions are defined in the <stdlib.h> header file.


malloc()
  • The name "malloc" stands for memory allocation.
  • The malloc() function reserves a block of memory of the specified number of bytes.
  • It returns a pointer of void which can be casted into pointers of any form.
  • It initializes each block with default garbage value.
Syntax: ptr = (castType*) malloc(size);

Example: ptr = (int*)malloc(10*sizeof(int));

The above statement allocates 40 bytes of memory. It's because the size of int is 4 bytes. And, the pointer ptr holds the address of the first byte in the allocated memory.

The expression results in a NULL pointer if the memory cannot be allocated.

calloc()
  • The name "calloc" stands for contiguous allocation.
  • The malloc() function allocates memory and leaves the memory uninitialized.
  • Whereas, the calloc() function allocates memory and initializes all bits to zero.
  • The calloc function has two parameters.
Syntax: ptr = (castType*) calloc(n, size);
Example: ptr = (int * ) calloc(10, int);

The above statement allocates contiguous memory for 2D elements of byte int

realloc():

  • If memory is not sufficient for malloc() or calloc(), we can reallocate the memory by realloc() function.
  • In short, it changes the memory size.
  • Re-allocation of memory maintains the already present value and new blocks will be initialized with default garbage value.


Syntax: ptr = realloc (ptr,newsize);

Here, ptr is reallocated with a new size

Example:  ptr = realloc (ptr,40*sizeof(int));


free()

  • “free” method in C is used to dynamically de-allocate the memory.
  • Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on their own.
  • We must explicitly use free() to release the space.
Syntax: free(ptr);This statement frees the space allocated in the memory pointed by ptr

Post a Comment

1 Comments

Anonymous said…
Interesting and Helpful Sir. Thanks