Sie sind auf Seite 1von 26

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : Murali Krishna Chintala
Designation : Lecturer in CME
Branch : Computer Engineering
Institute : SUVR & SR GPW, Ethamukkala
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM – 304
Topic : Basics of pointers
Duration : 50 Min
Sub Topic : Dynamic memory management functions
Teaching Aids : PPT, Animations

CM304.75 1
Objective

On completion of this period, you would be able to


Know …
Understand the following Dynamic memory
management functions with examples

 malloc()
 calloc()
 free()
 realloc()
CM304.75 2
Recap
In the previous class, we discussed about

 In dynamic allocation scheme storage is


allocated to a program and released back to
the computer while the program is
running,rather than fixed at compile time.

 Dynamic allocation scheme optimizes the


usage of storage space.

CM304.75 3
Dynamic Memory Allocation

Dynamic memory allocation can be done using


the following functions

 The malloc() function


 The calloc() function
 The free() function
 The realloc() function
CM304.75 4
Sizeof() Operator
Contd..
Before describing the above functions let us
first understand the sizeof operator.
 It is a unary operator that is used to obtain the
size of a data type or data object.
 It is not a library function but a keyword which
returns the size of the operand in bytes.
Example:
sizeof(char) is 1
sizeof(int) is 2
sizeof(float) is 4
CM304.75 5
malloc() function
 This is used to allocate a contiguous block of
memory in bytes and gives the starting
address to a pointer variable.
 Syntax:
ptr=(data-type *) malloc (size);
 where data-type says the type of pointer
returned by malloc() and size specifies the
required size of memory in bytes.

CM304.75 6
malloc() function

Example:
ptr=(int *) malloc(10);

On execution of this 10 bytes of memory space


is allocated and the starting address of the first
byte is assigned to the pointer ptr of type int.

CM304.75 7
malloc () function

Example:
ptr= (int *) malloc (10*size of(int));

 This allocates memory space 10 times the


size of an integer(i.e 10*2=20 bytes) and the
starting address of the first byte is assigned to
the pointer ptr of type integer.

CM304.75 8
malloc () function

 If sufficient memory is available, malloc()


allocates memory and returns the starting
address otherwise it returns null.

 Storage space allocated dynamically has no


name and therefore its contents can be
accessed only through a pointer.

CM304.75 9
Example program using malloc() to enter 10
numbers and print them
#include<stdio.h>
main()
{
int i,*ptr;
ptr=(int *)malloc(10*sizeof(int));
for(i=0;i<10;i++)
{
printf(“number %d”,i+1);
scanf(“%d”, (ptr+i);
}
CM304.75 10
Example program using malloc() to enter 10
numbers and print them
Contd..

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

printf(“%d\n”, *(ptr+i);

} /* end of main */

CM304.75 11
calloc() function

 This function is used to allocate multiple blocks


of contiguous memory in bytes.

 All blocks are of same size.

CM304.75 12
calloc() function

Syntax:
ptr=(data-type *) calloc(n,size);

 where ptr is a pointer variable already defined.


 data-type is the type of the pointer returned by
calloc.
 ‘n’ is the number of memory blocks.
 ‘size’ is the required size of each block.

CM304.75 13
calloc() function
Example:
ptr=(int *) calloc(5,2);

 This allocates 5 memory blocks of size 2


bytes and returns the starting address of the
first byte to the pointer variable ptr.

 calloc() function is generally used for


allocating memory space for arrays and
structures.

CM304.75 14
free() function
 This is used to free (release or de allocate) the
block of unused or already used memory.

Syntax:
free(ptr);

 Where ptr is a pointer variable that contains


base address of the memory block,created by
malloc() or calloc().

CM304.75 15
free() function
Example:
ptr= (int *)malloc((10);
free(ptr);
 The first statement allocates memory space of
10 bytes and returns the starting address to
pointer variable ptr.

 The second statement frees the allocated


memory.

CM304.75 16
realloc() function

 This function is used to increase or decrease the


size of memory already allocated by using
malloc() or calloc() function.

CM304.75 17
realloc() function
Syntax:
new-ptr= realloc(old-ptr,new-size);

 where new-ptr is a pointer variable already


defined.
 old-ptr is the pointer variable used in malloc() or
calloc().
 new-size is the size of new memory needed.

CM304.75 18
realloc() function

Example:
y=(int *) malloc(10);
x=realloc(y,30);

 First statement allocates memory size of 10


bytes and returns the starting address of the
memory to pointer variable y.

 Second statement reallocates already allocated


space to 30 bytes.
CM304.75 19
Summary
In this class, we have learnt about…
 Dynamic memory allocation can be done with
the help of the following 4 library functions
 malloc()
 calloc()
 realloc()
 free()
 The prototypes of all the above functions are
included in the header file
<stdlib.h>and<alloc.h>
CM304.75 20
Quiz

1.malloc() function is used to

a) Allocate a contiguous block of memory.

b) Multiple blocks of memory.

c) Reallocate the memory blocks.

CM304.75 21
Quiz

1.malloc() function is used to

a) Allocate a contiguous block of memory.

b) Multiple blocks of memory.

c) Reallocate the memory blocks.

CM304.75 22
Quiz

2.In case of insufficient memory, malloc() returns

a) Pointer to the first byte

b) null

c) Pointer to the last byte

CM304.75 23
Quiz

2.In case of insufficient memory, malloc() returns

a) Pointer to the first byte

b) null

c) Pointer to the last byte

CM304.75 24
Assignment

 Explain Dynamic memory management


features of ‘C’ language.

CM304.75 25
Frequently Asked Questions

2) Explain the use of malloc(), calloc(), free()


functions in ‘C’ language?
(Apr ’03,Oct ’04)

CM304.75 26