Data Strctures Unit 1
Data Strctures Unit 1
Dynamic memory allocation: Meaning of static & dynamic memory allocation; memory
allocation and de-allocation functions: malloc, calloc, realloc and free.
Introduction to Data Structures
•Data Structure can be defined as the group of data elements which provides
an efficient way of storing and organizing data in the computer so that it can
be used efficiently.
• Some examples of Data Structures are arrays, Linked List, Stack, Queue,
etc.
•Processor speed: To handle very large amount of data, high speed processing
is required, but as the data is growing day by day to the billions of files per
entity, processor may fail to deal with that much amount of data.
•There are better data structures which can make the search process
efficient like ordered array, binary search tree or hash tables.
Advantages of Data Structures
•Reusability: Data structures are reusable, i.e. once we have
implemented a particular data structure, we can use it at any other
place. Implementation of data structures can be compiled into libraries
which can be used by different clients.
•The client program uses the data structure through interface only,
without getting into the implementation details.
Classification of Data Structures
Classification of Data Structures
•Note:
•Due to its flexible nature, between the two major classification of
data structure, Non-primitive data structure is chosen more.
Operations on Data Structures
Memory allocation
•malloc()
•calloc()
•realloc()
•
Difference between Static and Dynamic Memory allocation
Dynamic Memory allocation
malloc() function
•Syntax
ptr=(cast-type*)malloc(byte-size)
•Example
Dynamic Memory allocation
calloc() function
•Syntax
ptr=(cast-type*)calloc(number, byte-size)
•Example
ptr=(int*)calloc(n,sizeof(int));
Dynamic Memory allocation
realloc() function
•If memory is not sufficient for malloc() or calloc(),
you can reallocate the memory by realloc() function.
ptr=realloc(ptr, new-size)
Dynamic Memory allocation
free() function
•The memory occupied by malloc() or calloc()
functions must be released by calling free() function.