1 of 12

Dynamic Memory Allocation

{C}

Programming

Programming for Problem Solving (PPS)

GTU # 3110003

USING

Prof. Nilesh Gambhava

Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot

2 of 12

Dynamic Memory Allocation (DMA)

  • If memory is allocated at runtime (during execution of program) then it is called dynamic memory. 
  • It allocates memory from heap (heap: it is an empty area in memory)
  • Memory can be accessed only through a pointer. 

When DMA is needed?

  • It is used when number of variables are not known in advance or large in size. 
  • Memory can be allocated at any time and can be released at any time during runtime.

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

2

3 of 12

malloc() function 

  • malloc () is used to allocate a fixed amount of memory during the execution of a program.
  • malloc () allocates size_in_bytes of memory from heap, if the allocation succeeds, a pointer to the block of memory is returned else NULL is returned.
  • Allocated memory space may not be contiguous.
  • Each block contains a size, a pointer to the next block, and the space itself. 
  • The blocks are kept in ascending order of storage address, and the last block points to the first.
  • The memory is not initialized.

Syntax

Description

ptr_var = (cast_type *) malloc (size_in_bytes);

This statement returns a pointer to size_in_bytes of uninitialized storage, or NULL if the request cannot be satisfied.

Example: fp = (int *)malloc(sizeof(int) *20);

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

3

4 of 12

Write a C program to allocate memory using malloc.

#include <stdio.h>

void main()

{

    int *fp; //fp is a pointer variable

    fp = (int *)malloc(sizeof(int)); //returns a pointer to int size storage

    *fp = 25//store 25 in the address pointed by fp

    printf("%d", *fp); //print the value of fp, i.e. 25

    free(fp); //free up the space pointed to by fp

}

1

2

3

4

5

6

7

8

9

Program

Output

25

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

4

5 of 12

calloc() function 

  • calloc() is used to allocate a block of memory during the execution of a program
  • calloc() allocates a region of memory to hold no_of_blocks of size_of_block each, if the allocation succeeds then a pointer to the block of memory is returned else NULL is returned. 
  • The memory is initialized to ZERO.

Syntax

Description

ptr_var = (cast_type *) calloc (no_of_blocks, size_of_block);

This statement returns a pointer to no_of_blocks of size size_of_blocks, it returns NULL if the request cannot be satisfied.

Example:

int n = 20;

fp = (int *)calloc(n, sizeof(int));

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

5

6 of 12

Write a C program to allocate memory using calloc.

#include <stdio.h>

void main()

{

    int i, n; //i, n are integer variables

    int *fp; //fp is a pointer variable

    printf("Enter how many numbers: "); 

    scanf("%d", &n);

    fp = (int *)calloc(n, sizeof(int)); //calloc returns a pointer to n blocks

    for(i = 0; i < n; i++)  //loop through until all the blocks are read

        scanf("%d",fp); //read and store into location where fp points

        fp++; //increment the pointer variable

    }

    free(fp); //frees the space pointed to by fp

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Program

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

6

7 of 12

realloc() function 

  • realloc() changes the size of the object pointed to by pointer fp to specified size. 
  • The contents will be unchanged up to the minimum of the old and new sizes. 
  • If the new size is larger, the new space will be uninitialized. 
  • realloc() returns a pointer to the new space, or NULL if the request cannot be satisfied, in which case *fp is unchanged. 

Syntax

Description

ptr_var = (cast_type *) realloc (void *fp,

size_t);

This statement returns a pointer to new space, or NULL if the request cannot be satisfied.

Example: fp = (int *)realloc(fp,sizeof(int)*20);

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

7

8 of 12

Write a C program to allocate memory using realloc.

#include <stdio.h>

void main()

{

    int *fp; //fp is a file pointer

    fp = (int *)malloc(sizeof(int)); //malloc returns a pointer to int size storage

    *fp = 25//store 25 in the address pointed by fp

    fp =(int *)realloc(fp, 2*sizeof(int)); //returns a pointer to new space

    printf("%d", *fp); //print the value of fp

    free(fp); //free up the space pointed to by fp

}

1

2

3

4

5

6

7

8

9

Program

Output

25

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

8

9 of 12

free() function 

  • free deallocates the space pointed to by fp.
  • It does nothing if fp is NULL.
  • fp must be a pointer to space previously allocated by calloc, malloc or realloc

Syntax

Description

void free(void *);

This statement free up the memory not needed anymore.

Example: free(fp);

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

9

10 of 12

Write a C program to sort numbers using malloc

            if(p[i] > p[j])

            {

                t = p[i];

                p[i] = p[j];

                p[j] = t;

            }

        } 

    }

    printf("Ascending order\n");

    for(i=0; i<n; i++)

        printf("%d\n", p[i]);

    free(p); 

}

#include<stdio.h>

#include<stdlib.h>

void main()

{

    int i,j,t,n;

    int *p;

    printf("Enter value of n: ");

    scanf("%d", &n);

    p=(int *) malloc(n * sizeof(int));

    printf("Enter values\n");

    for(i=0; i<n; i++)

        scanf("%d", &p[i]);

    for(i=0; i<n; i++)

    {

        for(j= i+1; j<n; j++)

        {

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Program

Program (cont.)

17

18

19

20

21

22

23

24

25

26

27

28

29

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

10

11 of 12

Write a C program to find square of numbers using calloc

Enter value of n: 3

Enter values

3

2

5

Square of 3 = 9

Square of 2 = 4

Square of 5 = 25

#include<stdio.h>

#include<stdlib.h>

void main()

{

    int i,n;

    int *p;

    printf("Enter value of n: ");

    scanf("%d",&n);

    p=(int*)calloc(n,sizeof(int));

    printf("Enter values\n");

    for(i=0;i<n;i++)

        scanf("%d",&p[i]);

    for(i=0;i<n;i++)

        printf("Square of %d = %d\n", p[i], p[i] * p[i]);

    free(p);

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Program

Output

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

11

12 of 12

Write a C program to add/remove item from a list using realloc

    

    printf("Enter new size of list: ");

    scanf("%d", &n2);

    

    fp = realloc(fp, n2 * sizeof(int));

    if(n2 > n1)

    {

        printf("Enter %d numbers\n", n2 - n1);

        for(i = n1; i < n2; i++)

            scanf("%d", &fp[i]);

    }

    printf("The numbers in the list are\n");

    for(i = 0; i < n2; i++)

        printf("%d\n", fp[i]);

}

#include<stdio.h>

#include<stdlib.h>

void main()

{

    int i, n1, n2;

    int *fp;

    printf("Enter size of list: ");

    scanf("%d", &n1);

    fp=(int *) malloc (n1 * sizeof(int));

    

    printf("Enter %d numbers\n", n1);

    for(i = 0; i < n1; i++)

        scanf("%d", &fp[i]);

    

    printf("The numbers in the list are\n");

    for(i = 0; i < n1; i++)

        printf("%d\n", fp[i]);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Program

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Program (cont.)

Prof. Nilesh Gambhava

#3110003 (PPS) – Introduction to C

12