Dynamic Memory Allocation
&
Linked Lists
Dynamic memory location:
The process of allocating memory at run time is known as dynamic memory
allocation.
C does not inherently have this facility. There are four library routines known as
memory management functions that can be used for allocating and freeing memory
during execution.
malloc(), calloc(), free(), realloc()
malloc: Allocating a block of memory
General form
ptr = (cast-type *) malloc(byte-size);
ptr is a pointer of type cast-type
The malloc() returns a pointer (of cast-type) to an area of memory with size byte-
size.
If there is not enough space a NULL pointer is returned.
Example
x=(int *) malloc(100*sizeof(int));
cptr=(char *) malloc(10);
st= (struct *) malloc(sizeof(struct store));
Storage space allocated dynamically has no name and therefore its contents can be accessed only through
a pointer.
calloc: Allocating multiple block of memory
While malloc() allocates a single block of storage space, calloc() allocates multiple blocks of storage, each
of the same size, and then set all bytes to zero. If there is not enough space a NULL pointer is returned.
General form
ptr=(cast-type *) calloc(n, element-size);
free( ); Releasing the used space
With dynamic run-time allocation, we need to release the space when it is not required
free(ptr);
Frees previously allocated space created by malloc() or calloc()
realloc( ); Altering the size of a block
It is likely that the previous allocated memory is not sufficient and we need
additional space for more elements.
It is also possible that the memory allocated is much larger than necessary and
we want to reduce it.
ptr=realloc(ptr, newsize);
Modifies the size of previously allocated space by malloc() or calloc().
Linked List
A list refers to a set of items organized sequentially
Linked list is a completely different way to represent a list is to make each item
in the list part of a structure that also contains a “link” to the structure
containing the next item.
A linked list is a dynamic data structures.
Dynamic Memory Allocation
Basic It is a consistent set of a fixed number of data
items.
It is an ordered set comprising a variable number
of data items.
Size Specified during declaration. No need to specify; grow and shrink during
execution
Storage
Allocation
Element location is allocated during compile time. Element position is assigned during run time.
Order of the
elements
Stored consecutively Stored randomly
Accessing the
element
Direct or randomly accessed, i.e., Specify the array
index or subscript.
Sequentially accessed, i.e., Traverse starting from
the first node in the list by the pointer.
Difference between array and linked list
Array Linked List
Insertion and
deletion of
element
Slow relatively as shifting is required. Easier, fast and efficient.
Searching Binary search and linear search linear search
Memory
required
less More
Memory
Utilization
Ineffective Efficient
Difference between array and linked list
Array Linked List
Advantages of linked list
• Can grow or shrink in size during the execution of a program
• Does not waste memory space
• Here it is not necessary to specify the number of nodes to be used in the list
• It provides flexibility is allowing the items to be rearranged efficiently
• It is easier to insert or delete items by rearranging the links
Limitation of Linked List
• The access to any arbitrary item is little cumbersome and time consuming.
• Use more storage than an array with the same number of items. This is because each item
has an additional link field.
• Linked lists are traversed in unidirection
• Complex to implement
• Sequential access to elements
• Leads to memory problems if not taken care about the pointer manipulations properly
That is, Whenever we deal with a fixed length list, it would be better to use an array rather
than a linked list.
What we can do with Linked List:-
1. Creating a list.
2. Traversing the list.
3. Counting the items in the list.
4. Printing the list (or sub list).
5. Looking up an item for editing or printing.
6. Inserting an item.
7. Deleting an item.
8. Concatenating two lists.
9. Replace() : replaces a value stored at a position with some other value.
10.Swap() : swap the values of the nodes specified by two positions.
11. Merging two linked lists into a larger list.
12.Searching for an element in a linked list.
13.Reversing a linked list.

Dynamic memory allocation and linked lists

  • 1.
  • 2.
    Dynamic memory location: Theprocess of allocating memory at run time is known as dynamic memory allocation. C does not inherently have this facility. There are four library routines known as memory management functions that can be used for allocating and freeing memory during execution. malloc(), calloc(), free(), realloc() malloc: Allocating a block of memory General form ptr = (cast-type *) malloc(byte-size); ptr is a pointer of type cast-type The malloc() returns a pointer (of cast-type) to an area of memory with size byte- size. If there is not enough space a NULL pointer is returned.
  • 3.
    Example x=(int *) malloc(100*sizeof(int)); cptr=(char*) malloc(10); st= (struct *) malloc(sizeof(struct store)); Storage space allocated dynamically has no name and therefore its contents can be accessed only through a pointer. calloc: Allocating multiple block of memory While malloc() allocates a single block of storage space, calloc() allocates multiple blocks of storage, each of the same size, and then set all bytes to zero. If there is not enough space a NULL pointer is returned. General form ptr=(cast-type *) calloc(n, element-size); free( ); Releasing the used space With dynamic run-time allocation, we need to release the space when it is not required free(ptr); Frees previously allocated space created by malloc() or calloc()
  • 4.
    realloc( ); Alteringthe size of a block It is likely that the previous allocated memory is not sufficient and we need additional space for more elements. It is also possible that the memory allocated is much larger than necessary and we want to reduce it. ptr=realloc(ptr, newsize); Modifies the size of previously allocated space by malloc() or calloc(). Linked List A list refers to a set of items organized sequentially Linked list is a completely different way to represent a list is to make each item in the list part of a structure that also contains a “link” to the structure containing the next item. A linked list is a dynamic data structures.
  • 5.
    Dynamic Memory Allocation BasicIt is a consistent set of a fixed number of data items. It is an ordered set comprising a variable number of data items. Size Specified during declaration. No need to specify; grow and shrink during execution Storage Allocation Element location is allocated during compile time. Element position is assigned during run time. Order of the elements Stored consecutively Stored randomly Accessing the element Direct or randomly accessed, i.e., Specify the array index or subscript. Sequentially accessed, i.e., Traverse starting from the first node in the list by the pointer. Difference between array and linked list Array Linked List
  • 6.
    Insertion and deletion of element Slowrelatively as shifting is required. Easier, fast and efficient. Searching Binary search and linear search linear search Memory required less More Memory Utilization Ineffective Efficient Difference between array and linked list Array Linked List
  • 7.
    Advantages of linkedlist • Can grow or shrink in size during the execution of a program • Does not waste memory space • Here it is not necessary to specify the number of nodes to be used in the list • It provides flexibility is allowing the items to be rearranged efficiently • It is easier to insert or delete items by rearranging the links Limitation of Linked List • The access to any arbitrary item is little cumbersome and time consuming. • Use more storage than an array with the same number of items. This is because each item has an additional link field. • Linked lists are traversed in unidirection • Complex to implement • Sequential access to elements • Leads to memory problems if not taken care about the pointer manipulations properly That is, Whenever we deal with a fixed length list, it would be better to use an array rather than a linked list.
  • 8.
    What we cando with Linked List:- 1. Creating a list. 2. Traversing the list. 3. Counting the items in the list. 4. Printing the list (or sub list). 5. Looking up an item for editing or printing. 6. Inserting an item. 7. Deleting an item. 8. Concatenating two lists. 9. Replace() : replaces a value stored at a position with some other value. 10.Swap() : swap the values of the nodes specified by two positions. 11. Merging two linked lists into a larger list. 12.Searching for an element in a linked list. 13.Reversing a linked list.