Unit 5(Part 2)Dynamic Memory Allocation
Unit 5(Part 2)Dynamic Memory Allocation
Memory Allocation
There are 2 types of memory allocations in c
1) Static Allocation:
In static memory allocation, the allocated memory is fixed.
Once the memory is allocated, it cannot be changed.
The memory cannot be increased or decreased.
2) Dynamic allocation:
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-2)Dynamic Memory Allocation
implement. implement.
In static memory allocation, cannot reuse Dynamic memory allocation allows reusing
the unused memory. the memory. The programmer can allocate
more memory when required . He can
release the memory when necessary.
In static memory allocation, it is not possible In dynamic memory allocation, the memory
to resize after initial allocation. can be minimized or maximize accordingly.
Ex:
int *ptr;
ptr=malloc(20*sizeof(int));
or
ptr=malloc(20, 2);
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-2)Dynamic Memory Allocation
int *ptr;
ptr=(int *)malloc(20*sizeof(int));
The starting address of this block is assigned to ptr.
The above statement allocates 40 bytes of memory. It's because the size of int is 2
bytes. And, the pointer ptr holds the address of the first byte in the allocated
memory.(Consecutive memory)
The expression results in a NULL pointer if the memory cannot be allocated.
ex:
char *ptr;
ptr=(char *)malloc(20*sizeof(int));
Here 2 specifies the size of data type in byte for which we want the allocation to be
made, which is in this case is 2 for integers and 10 specify the number of elements
for which allocation is to be made.
The above statement allocates 40 bytes of memory. It's because the size of int is 2
bytes. And, the pointer ptr holds the address of the first byte in the allocated
memory.
Note: 1) The malloc() function allocates memory and leaves the memory
uninitialized. Whereas, the calloc() function allocates memory and initializes all bits
to zero.
iii) free():
The free function is used to de-allocate the prviously allocated memory using
malloc() or calloc() function.
Syntax:
free(pointer_variable);
ex:
int *ptr;
ptr=(int *)malloc(20*sizeof(int));
free(ptr);
3
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-2)Dynamic Memory Allocation
This statement frees the space allocated in the memory pointed by ptr.
iv) realloc():
This function is used to resize the size of memory block, which is already
allocated.
Syntax:
ralloc(pointer_variable, new_size);
ex:
ptr=realloc(ptr,new_size);
calloc() malloc()
calloc() initializes the allocated memory malloc() initializes the allocated memory with
with 0 value. garbage values.
Syntax : Syntax :
calloc(number of elements, size of each malloc(number of elements * size of each
element); element);
Linked List:
A linked list is a collection of nodes, arranged in such a way that each node contains one
value and one pointer. The pointer always points to the next member of the list. If the
pointer is NULL, then it is the last node in the list.
or
A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers as shown in the
below image:
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-2)Dynamic Memory Allocation
In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
It is the simplest type of linked list in which every node contains some data and a pointer
to the next node of the same data type. The node contains a pointer to the next node
means that the node stores the address of the next node in the sequence. A single linked
list allows traversal of data only in one way.
A circular linked list is that in which the last node contains the pointer to the first node of
the list. While traversing a circular liked list, we can begin at any node and traverse the
list in any direction forward and backward until we reach the same node we started. Thus,
a circular linked list has no beginning and no end.
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-2)Dynamic Memory Allocation
A doubly linked list or a two-way linked list is a more complex type of linked list which
contains a pointer to the next as well as the previous node in sequence, Therefore, it
contains three parts are data, a pointer to the next node, and a pointer to the previous
node. This would enable us to traverse the list in the backward direction as well.
A Doubly Circular linked list or a circular two-way linked list is a more complex type of
linked-list that contains a pointer to the next as well as the previous node in the
sequence. The difference between the doubly linked and circular doubly list is the same as
that between a singly linked list and a circular linked list. The circular doubly linked list
does not contain null in the previous field of the first node.
Self Referential Structure is the Data Structure in which the pointer refers (points) to
the structure of the same type. It used in the many of the data structures like in Linked
List, Trees, Graphs, Heap etc.
@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-2)Dynamic Memory Allocation
Self Referential Structure with Single Link: These structures can have only one self-
pointer as their member. The following example will show us how to connect the objects of
a self-referential structure with the single link and access the corresponding data members.
The connection formed is shown in the following figure.
Self Referential Structure with Multiple Links: Self referential structures with multiple
links can have more than one self-pointers. Many complicated data structures can be easily
constructed using these structures. Such structures can easily connect to more than one
nodes at a time. The following example shows one such structure with more than one links.
The connections made in the above example can be understood using the following figure.
@Jeetesh Srivastava(UCEM-CSE)