0% found this document useful (0 votes)
39 views

Unit 5(Part 2)Dynamic Memory Allocation

Uploaded by

rameshjaiswal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Unit 5(Part 2)Dynamic Memory Allocation

Uploaded by

rameshjaiswal365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit-5(Part-2)Dynamic Memory Allocation

Memory Allocation
There are 2 types of memory allocations in c

1) Compile time or static allocation


2) Run Time or Dynamic allocation(using pointers)

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.

Ex: int x [5];


This x is an array which can store a sequence of data which are of the same type. It can
store five integer elements. It cannot store more than five elements.
Ex:
If the programmer allocated an array that can store 10 elements, it is not possible to store
values more than that specified amount.
Ex:
If the programmer initially allocated an array that can hold 10 elements, but only needed 5
elements, then there is a memory wastage. That memory is no longer needed, but it is also
not possible to reuse the memory. Static memory allocation is fixed but the implementation
is simple and easy, and it is also fast.

2) Dynamic allocation:

 Sometimes it is necessary to change the size of the memory. So memory can


be allocated dynamically.

 In dynamic allocation, the required memory size is allocated while program is


executing.
The main advantage of dynamic memory allocation is that it saves memory. The
programmer can allocate memory or release the memory as necessary. Memory can be
reallocated during execution and can free the memory when it is not required. Dynamic
memory allocation is also efficient than static memory allocation. One disadvantage is that
implementing dynamic memory allocation is complex.

Difference between Static and Dynamic Memory Allocation:

Static memory allocation is a method of Dynamic memory allocation is a method


allocating memory, and once the of allocating memory, and once the
memory is allocated, it is fixed. memory is allocated, it can be changed.

Static memory allocation is easy to Dynamic memory allocation is complex to


1

@Jeetesh Srivastava(UCEM-CSE)
Unit-5(Part-2)Dynamic Memory Allocation

implement. implement.

In static memory, allocation execution is In dynamic memory, allocation execution is


faster than dynamic memory allocation. slower than static memory allocation.

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.

Dynamic Memory Allocation:


The process of allocating memory during program execution is called dynamic
memory allocation.

c provide the following dynamic allocation and de-allocation functions:


i) malloc()
ii) calloc()
iii) free()
iv) realloc()

i) malloc():(The name "malloc" stands for memory allocation.)


The malloc function allocates a block of memory in bytes. The malloc()
function is like a request to the RAM of the system to allocate memory, if the request
is granted, returns a pointer to the first block of that memory. The type of pointer it
returns is void, which means that we can assign it any type of pointer. However if
the malloc() function fails to allocate the required amount of memory. It returns a
NULL. The malloc function is available in header file alloc.h or stdlib.h in Turbo C.
The syntax of this function is as follows:
malloc(number of elements * size of each element);

Ex:
int *ptr;
ptr=malloc(20*sizeof(int));
or
ptr=malloc(20, 2);

Note: Function malloc() returns a void pointer so a cast operator is required to


change the returned pointer type according to our need, the above declaration would
take the following form:
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));

ii) calloc():(The name "calloc" stands for contiguous allocation.)


This function works exactly similar to malloc() function except for the fact
that it needs 2 arguments as against one argument required by malloc().
The calloc function is also available in header file alloc.h or stdlib.h in Turbo C.
Syntax:
calloc(number of elements, size of each element);
ex:
int *ptr;
ptr=(int *)calloc(20, sizeof(int));
or
ptr=(int *)calloc(20, 2);

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);

Diffrence between malloc() and calloc():

calloc() malloc()

calloc() initializes the allocated memory malloc() initializes the allocated memory with
with 0 value. garbage values.

Number of arguments is 2 Number of argument is 1

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.

There are 4 types of linked list

1) Singly Linked List

2) Circular Linked List

3) Doubly Linked List

4) Doubly circular Linked List

1) Singly Linked 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.

2) Circular Linked List:

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

3) Doubly Linked List:

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.

4) Doubly circular Linked List:

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:

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

Types of Self Referential Structures


1. Self Referential Structure with Single Link
2. Self Referential Structure with Multiple Links

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)

You might also like