Chapter 9
Chapter 9
CHAPTER 9
Dynamic memory allocation
Q.1 Recall the definition of dynamic memory allocation? Differentiate between malloc and
calloc? (CPU_WIN_2014)
Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure
(like Array) is changed during the runtime.
BASIS OF
MALLOC() CALLOC()
COMPARISON
Q. 2 Explain the use if malloac and calloc function with their syntax. (CPU_SUM_2015,17,18)
malloc()
This function allocates a size byte of memory. It returns a pointer (*) to the first byte, or if there is
an error, it returns NULL (to ensure that the situation is out of memory). The format follows as:
(type *) malloc(sizeof(type));
type can be any variable type, such as int, char, float, etc…
#include <stdio.h>
CKPCET, SURAT 1
PPS (3110003)
#include <stdlib.h>
int main(void)
int *p;
p = 0x42;
p = (int *)malloc(sizeof(int));
return (0);
calloc()
Same principle as malloc(), but it’s used to allocate storage. The real difference between these two,
is that calloc() initializes all bytes in the allocation block to zero, because it’s used to reserve space
for dynamic arrays. It’s written like this.
num specifies the size in bytes of one element to the second argument (size). If the partitioning is
successful, the address is returned. If not, NULL is returned.
#include <stdio.h>
#include <stdlib.h>
CKPCET, SURAT 2
PPS (3110003)
if (i > size)
printf("[%d]", array[i]);
else
putchar('\n');
int main(void)
int *ptr_calloc;
int *ptr_malloc;
print_array(ptr_calloc, NUM_ELEM);
print_array(ptr_malloc, NUM_ELEM);
return (0);
CKPCET, SURAT 3
PPS (3110003)
Function Purpose
malloc Allocates the memory of requested size and returns the pointer to the first byte of
allocated space.
calloc Allocates the space for elements of an array. Initializes the elements to zero and
returns a pointer to the memory.
Advantages:
· Dynamic Allocation is done at run time.
· Data structures can grow and shrink to fit changing data requirements.
· We can allocate (create) additional storage whenever we need them.
· We can de-allocate (free/delete) dynamic space whenever we are done with them.
· Thus we can always have exactly the amount of space required - no more, no less.
Disadvantages:
· As the memory is allocated during runtime, it requires more time.
· Memory needs to be freed by the user when done. This is important as it is more likely to turn
into bugs that are difficult to find.
CKPCET, SURAT 4
PPS (3110003)
MCQ
(CPU_WIN_2017)
Q. Which function should be used to release allocated memory which is not needed?
a) dealloc( ) b) free ( ) c) release ( ) d) unalloc ( )
(CPU_SUM_2015)
Q. Difference between calloc() and malloc()
a) calloc() takes a single argument while malloc() needs two arguments
b) malloc() takes a single argument while calloc() needs two arguments
c) malloc() initializes the allocated memory to ZERO
d) calloc() initializes the allocated memory to NULL
(CPU_SUM_2019)
Q. The Function __________ obtains block of memory run time.
(a) malloc() (b) calloc() (c) Both malloc() and calloc() (d) free()
CKPCET, SURAT 5