0% found this document useful (0 votes)
17 views5 pages

Chapter 9

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Chapter 9

Uploaded by

Priyansh Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PPS (3110003)

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

No of blocks Assigns single block of Assigns multiple blocks of the


demanded memory. requested memory.

Syntax void *malloc(size_t size); void *calloc(size_t num, size_t size);

Initialization malloc() doesn't clear and The allocated memory is initialized


initialize the allocated memory. to zero by using calloc().

Manner of Allocation malloc() function allocates calloc() function allocates memory


memory of size 'size' from the the size of which is equal to num
heap. *size.

Speed Fast Comparatively slow.

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;

printf("Pointer before malloc(): %p\n", p);

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

printf("Pointer after malloc(): %p\n", p);

return (0);

The output should be something like this:

Pointer before malloc(): 0x42

Pointer after malloc(): 0x53fe040

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.

(type *) calloc(num, size);

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>

#define NUM_ELEM (5)

void print_array(int *array, int size)

CKPCET, SURAT 2
PPS (3110003)

for (int i = 0; i < size; i += 1)

if (i > size)

printf("[%d]", array[i]);

else

printf("[%d], ", array[i]);

putchar('\n');

int main(void)

int *ptr_calloc;

int *ptr_malloc;

ptr_calloc = (int*) calloc(NUM_ELEM, sizeof(int));

ptr_malloc = (int*) malloc(sizeof(int) * NUM_ELEM);

print_array(ptr_calloc, NUM_ELEM);

print_array(ptr_malloc, NUM_ELEM);

return (0);

CKPCET, SURAT 3
PPS (3110003)

Q. 3 Explain dynamic memory allocation. (CPU_SUM_2016,18) (PPS_SUM_2019)


Describe dynamic memory allocation. Give advantages and disadvantages of it.
(CPU_SUM_2019)
Dynamic memory allocation is an aspect of allocating and freeing memory according to your
needs. Dynamic memory is managed and served with pointers that point to the newly allocated
space of memory in an area which we call the heap. Now you can create and destroy an array of
elements at runtime without any problems.
To sum up, the automatic memory management uses the stack, and the dynamic memory allocation
uses the heap.
The <stdlib.h> library has functions responsible for dynamic memory management.

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.

realloc It is used to modify the size of previously allocated memory space.

Free Frees or empties the previously allocated memory space.

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_WIN_2016) (CPU_WIN_2014) (CPU_WIN_2016)


Q.Which function reallocates memory?
(a) realloc (b) calloc (c) malloc (d) None of these

(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

You might also like