Introduction to Lists and Linked Lists & Dynamic memory allocation

 

  • Linked List can be defined as collection of objects called nodes that are randomly stored in the memory.
  • A node contains two fields i.e. data stored at that particular address and the pointer which contains the address of the next node in the memory.
  • The last node of the list contains pointer to the null.


Dynamic memory allocation in C

The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime. Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

Before learning above functions, let's understand the difference between static memory allocation and dynamic memory allocation.

static memory allocationdynamic memory allocation
memory is allocated at compile time.memory is allocated at run time.
memory can't be increased while executing program.memory can be increased while executing program.
used in array.used in linked list.

Now let's have a quick look at the methods used for dynamic memory allocation.

malloc()allocates single block of requested memory.
calloc()allocates multiple block of requested memory.
realloc()reallocates the memory occupied by malloc() or calloc() functions.
free()frees the dynamically allocated memory.


No comments:

Post a Comment