Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Dynamic Memory Management Functions: malloc, calloc, free, realloc, Essays (university) of Compiler Design

An introduction to dynamic memory management functions in C programming, including malloc, calloc, free, and realloc. It explains how to use these functions to allocate and deallocate memory dynamically, as well as how to change the size of previously allocated memory using realloc.

Typology: Essays (university)

2020/2021

Uploaded on 01/08/2021

maneesh-reddy
maneesh-reddy 🇮🇳

4 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DATA STRUCTURES AND APPLICATIONS
15CS33
Module -1
Introduction
Teaching Hours: 10
Pg no
1.
2
2
3
3
10
2.
11
14
20
22
3.
23
26
4.
26
Dept. of CSE, ACE Page 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Dynamic Memory Management Functions: malloc, calloc, free, realloc and more Essays (university) Compiler Design in PDF only on Docsity!

Introduction

 - Module - - Teaching Hours: 
    1. Introduction to Data Structures CONTENTS Pg no
    • 1.1. Classification of Data Structures
    • 1.2. Data structure Operations
    • 1 .3. Review of Structures Unions and Pointers
    • 1.4. Self Referential Structures
    1. Arrays
    • 2.1. Operations
    • 2.2. Multidimensional Arrays
    • 2.3. Applications of Arrays
    1. Strings
    • 3.1. String manipulation Applications
    1. Dynamic Memory Management Functions

1. Introduction to Data Structures Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective way. Data Structures is about rendering data elements in terms of some relationship, for better organization and storage. For example, we have data player's name "Virat" and age 26. Here "Virat" is of String data type and 26 is of integer data type 1.1. Classification of Data Structures Data structures can be classified as · Simple data structure · Compound data structure · Linear data structure · Non linear data structure Simple Data Structure: Simple data structure can be constructed with the help of primitive data structure. A primitive data structure used to represent the standard data types of any one of the computer languages. Variables, arrays, pointers, structures, unions, etc. are examples of primitive data structures. Compound Data structure: Compound data structure can be constructed with the help of any one of the primitive data structure and it is having a specific functionality. It can be designed by user. It can be classified as

  1. Linear data structure
  2. Non-linear data structure

 member definition;  } [one or more structure variables]; Accessing Structure Members To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type. The following example shows how to use a structure in a program – #include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main( ) { struct Books Book1; /* Declare Book1 of type Book / struct Books Book2; / Declare Book2 of type Book / / book 1 specification / strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; / book 2 specification */ strcpy( Book2.title, "Telecom Billing"); strcpy( Book2.author, "Zara Ali");

strcpy( Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* print Book1 info / printf( "Book 1 title : %s\n", Book1.title); printf( "Book 1 author : %s\n", Book1.author); printf( "Book 1 subject : %s\n", Book1.subject); printf( "Book 1 book_id : %d\n", Book1.book_id); / print Book2 info */ printf( "Book 2 title : %s\n", Book2.title); printf( "Book 2 author : %s\n", Book2.author); printf( "Book 2 subject : %s\n", Book2.subject); printf( "Book 2 book_id : %d\n", Book2.book_id); return 0; } UNIONS A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose. Defining a Union To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows − union [union tag] { member definition; member definition; ... member definition;

printf( "Memory size occupied by data : %d\n", sizeof(data)); return 0; } When the above code is compiled and executed, it produces the following result − Memory size occupied by data : 20 Accessing Union Members To access any member of a union, we use the member access operator (.). The member access operator is coded as a period between the union variable name and the union member that we wish to access. You would use the keyword union to define variables of union type. The following example shows how to use unions in a program − #include <stdio.h> #include <string.h> union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; data.f = 220.5; strcpy( data.str, "C Programming");

printf( "data.i : %d\n", data.i); printf( "data.f : %f\n", data.f); printf( "data.str : %s\n", data.str); return 0; POINTERS Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let's start learning them in simple and easy steps. As you know, every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following example, which prints the address of the variables defined − #include <stdio.h> int main () { int var1; char var2[10]; printf("Address of var1 variable: %x\n", &var1 ); printf("Address of var2 variable: %x\n", &var2 ); return 0; } When the above code is compiled and executed, it produces the following result − Address of var1 variable: bff5a Address of var2 variable: bff5a3f What are Pointers?

printf("Address of var variable: %x\n", &var ); /* address stored in pointer variable / printf("Address stored in ip variable: %x\n", ip ); / access the value using the pointer */ printf("Value of *ip variable: %d\n", *ip ); return 0; } When the above code is compiled and executed, it produces the following result − Address of var variable: bffd8b3c Address stored in ip variable: bffd8b3c Value of *ip variable: 20 1.4. Self Referential Structures A self referential structure is used to create data structures like linked lists, stacks, etc. Following is an example of this kind of structure: struct struct_name { datatype datatypename; struct_name * pointer_name; }; A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a self-referential data structure. The next node of a node is being pointed, which is of the same struct type. For example, typedef struct listnode { void *data; struct listnode *next; } linked_list;

In the above example, the listnode is a self-referential structure – because the *next is of the type struct listnode.

2. Arrays In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students. This can be done by creating 100 variable individually but, this process is rather tedious and impracticable. These type of problem can be handled in C programming using arrays. An array is a sequence of data item of homogeneous value(same type). Arrays are of two types: One-dimensional arrays Multidimensional arrays( will be discussed in next chapter ) Declaration of one-dimensional array data_type array_name[array_size]; For example: int age[5]; Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All element in an array are of the same type (int, in this case). Array elements Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program. For example: int age[5]; Note that, the first element is numbered 0 and so on. Here, the size of array age is 5 times the size of int because there are 5 elements. Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on. Initialization of one-dimensional array: Arrays can be initialized at declaration time in this source code as:

scanf("%d",&n); for(i=0;i<n;++i){ printf("Enter marks of student%d: ",i+1); scanf("%d",&marks[i]); sum+=marks[i]; } printf("Sum= %d",sum); return 0; } Output Enter number of students: 3 Enter marks of student1: 12 Enter marks of student2: 31 Enter marks of student3: 2 sum= 2.1 Operations Insertion Operation Insert operation is to insert one or more data elements into an array. Based on the requirement, new element can be added at the beginning, end or any given index of array. Here, we see a practical implementation of insertion operation, where we add data at the end of the array − Algorithm Let Array is a linear unordered array of MAX elements. Example Result Let LA is a Linear Array (unordered) with N elements and K is a positive integer such that K<=N. Below is the algorithm where ITEM is inserted into the K th position of LA −

  1. Start
  2. Set J=N
  3. Set N = N+
  4. Repeat steps 5 and 6 while J >= K
  5. Set LA[J+1] = LA[J]
  6. Set J = J- 1
  7. Set LA[K] = ITEM
  8. Stop Example Below is the implementation of the above algorithm − #include <stdio.h> main() { int LA[] = {1,3,5,7,8}; int item = 10, k = 3, n = 5; int i = 0, j = n; printf("The original array elements are :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } n = n + 1; while( j >= k){ LA[j+1] = LA[j]; j = j - 1; } LA[k] = item;
  1. Stop Example Below is the implementation of the above algorithm − #include <stdio.h> main() { int LA[] = {1,3,5,7,8}; int k = 3, n = 5; int i, j; printf("The original array elements are :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } j = k; while( j < n){ LA[j-1] = LA[j]; j = j + 1; } n = n - 1; printf("The array elements after deletion :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } }

When compile and execute, above program produces the following result − The original array elements are : LA[0]= LA[1]= LA[2]= LA[3]= LA[4]= The array elements after deletion : LA[0]= LA[1]= LA[2]= LA[3]= Search Operation You can perform a search for array element based on its value or its index. Algorithm Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Below is the algorithm to find an element with a value of ITEM using sequential search.

  1. Start
  2. Set J=
  3. Repeat steps 4 and 5 while J < N
  4. IF LA[J] is equal ITEM THEN GOTO STEP 6
  5. Set J = J +
  6. PRINT J, ITEM
  7. Stop Example Below is the implementation of the above algorithm − #include <stdio.h> main() { int LA[] = {1,3,5,7,8}; int item = 5, n = 5; int i = 0, j = 0;
  1. Start
  2. Set LA[K-1] = ITEM
  3. Stop Example Below is the implementation of the above algorithm − #include <stdio.h> main() { int LA[] = {1,3,5,7,8}; int k = 3, n = 5, item = 10; int i, j; printf("The original array elements are :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } LA[k-1] = item; printf("The array elements after updation :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } } When compile and execute, above program produces the following result − The original array elements are : LA[0]= LA[1]= LA[2]=

LA[3]=

LA[4]=

The array elements after updation : LA[0]= LA[1]= LA[2]= LA[3]= LA[4]= 2.2 Multidimensional Arrays C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration − type name[size1][size2]...[sizeN]; For example, the following declaration creates a three dimensional integer array − int threedim[5][10][4]; Two-dimensional Arrays The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y], you would write something as follows − type arrayName [ x ][ y ]; Where type can be any valid C data type and arrayName will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns. A two-dimensional array a, which contains three rows and four columns can be shown as follows − Thus, every element in the array a is identified by an element name of the forma[ i ][ j ], where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in 'a'.