Professional Documents
Culture Documents
5.4 Dynamic Memory Allocation - Singly Linked List - Typedef
5.4 Dynamic Memory Allocation - Singly Linked List - Typedef
UNIT NO. 5
STRUCTURES AND FILE PROCESSING
20ESCS101
Problem Solving and Programming in C
(Common to ALL Departments)
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Dynamic Memory Allocation: Ability of a program to use more memory space at execution time
❖ Memory space required can be specified at the time of execution.
❖ C supports allocating and freeing memory dynamically using library routines.
❖ Hold new nodes -Use function malloc to allocate memory.
❖ Release space no longer needed -Use function free to deallocate memory
❖ Use #include <stdlib.h> header file when using malloc & free functions.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
❖ When we do not know how much amount of memory would be needed for the program
beforehand.
❖ When we want data structures without any upper limit of memory space.
❖ When we want to use our memory space more efficiently.Example: If we have allocated
memory space for a 1D array as array[20] and you end up using only 10 memory spaces
then the remaining 10 memory spaces would be wasted and this wasted memory cannot
even be utilized by other program variables.
❖ Dynamically created lists insertions and deletions can be done very easily just by the
manipulation of addresses whereas in case of statically allocated memory insertions and
deletions lead to more movements and wastage of memory.
❖ When we want you to use the concept of structures and linked list in programming,
dynamic memory allocation is a must.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
❖ malloc() – Allocates requested number of bytes and returns a pointer to the first byte
of the allocated space.
❖ calloc()– Allocates space for an array of elements, initializes them to zero and then
returns a pointer to the memory.
❖ free() Frees previously allocated space.
❖ realloc()– Modifies the size of previously allocated space.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Malloc()
The name "malloc" stands for memory allocation.This function reserves a block of memory of the
specified number of bytes , its declaration is void*malloc(size);
malloc () returns the pointer to the 1st byte and allocate memory, and its return type is void,
which can be type casted as: int *p=(datatype*)malloc(size).
If memory allocation is successful, it returns the address of the memory chunk that was allocated
and it returns null on unsuccessful allocation .Example: int*p=(int*)malloc(10);
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Calloc()
calloc” or “contiguous allocation” method in C is used to dynamically allocate the
specified number of blocks of memory of the specified type. It initializes each block with a
default value ‘0’.
Syntax:
ptr = (cast-type*)calloc(n, element-size);
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of
the float.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Free()
“free” method in C is used to dynamically de-allocate the memory. The memory allocated
using functions malloc() and calloc() is not de-allocated on their own. Hence the free()
method is used, whenever the dynamic memory allocation takes place. It helps to reduce
wastage of memory by freeing it.
Syntax: free(ptr);
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Realloc()
Typedef
❖ typedef is a keyword used in C language to assign alternative names to existing data types.
❖ It's mostly used with user defined datatypes, when names of the datatypes become
slightly complicated to use in programs.
❖ General syntax for using typedef: typedef <existing_name> <alias_name>
❖ Following is an example to define a term BYTE for one-byte numbers –
❖ After this type definition, the identifier BYTE can be used as an abbreviation for the type
unsigned char, for example.. BYTE b1, b2;
❖ By convention, uppercase letters are used for these definitions to remind the user that the
type name is really a symbolic abbreviation, but we can use lowercase, as follows − typedef
unsigned char byte;
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
❖ Application of typedef -It can be used to give a name to user defined data type as
well.
For example:
typedef struct
{ type member1;
type member2;
type member3;
} type_name;
Here type_name represents the structure definition associated with it. Now this
type_name can be used to declare a variable of this structure type.type_name t1, t2;
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
❖ We can use typedef to give a name to your user defined data types as well. For example,
we can use typedef with structure to define a new data type and then use that data type to
define structure variables directly as follows –
#include<stdio.h>
#include<string.h>
char name[50];
int salary;
}emp;
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
void main( )
emp e1;
printf("\nEmployee name:\t");
scanf("%s", e1.name);
scanf("%d", &e1.salary);
int* x, y;
IntPtr x, y, z;
❖ But if we use typedef like we have used in the example above, we can declare
any number of pointers in a single statement.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
typedef vs #define
❖ #define is a C-directive which is also used to define the aliases for various data types
similar to typedef but with the following differences −
❏ typedef is limited to giving symbolic names to types only whereas #define can be used
to define alias for values as well, q.,we can define 1 as ONE etc.
❏ typedef interpretation is performed by the compiler whereas #define statements are
processed by the pre-processor.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
LINKED LIST
Linked List is a linear data structure. Unlike arrays, linked list elements are not stored
● Link − Each link of a linked list can store a data called an element.
● Next − Each link of a linked list contains a link to the next link called Next.
● LinkedList − A Linked List contains the connection link to the first link called First.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Limitation of arrays
Arrays can be used to store linear data of similar types, but arrays have the following limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the upper limit
2) Inserting a new element in an array of elements is expensive because the room has to be
created for the new elements and to create room existing elements have to be shifted.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Basic Operations
● Insertion
○ Adds an element at the beginning of the list.
○ Adds an element at the middle of the list.
○ Adds an element at the end of the list.
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
● Deletion
○ Deletes an element at the beginning of the list.
○ Deletes an element at the middle of the list
○ Deletes an element at the end of the list
● Display − Displays the complete list.
● Search − Searches an element using the given key.
int main()
Sample Program-Singly Linked list
#include <stdio.h> {int n,num,pos;
void displayList(); //function to printf("\n Data, after deletion of first node : \n");
display the list
displayList();
return 0; }
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
else
{ printf(" Memory can not be
allocated."); { printf(" Input data for node %d : ", i);
{ {
printf(" There are no node in the list."); printf(" No data found in the list.");
} }
else else
{ { tmp = stnode;
stnode = stnode->nextptr; {
printf("\n Data of node 1 which is being deleted is : printf(" Data = %d\n", tmp->num);
%d\n", toDelptr->num);
tmp = tmp->nextptr; }
free(toDelptr); } }
}
}
20ESCS101
PROBLEM SOLVING AND PROGRAMMING IN C (Common to All Departments )
Video link
1. https://www.youtube.com/watch?v=udfbq4M2Kfc
2. https://www.youtube.com/watch?v=HrY_YmU1vdg