Docsity

Dynamic Memory Allocation, Summaries of Software Engineering

Partial preview of the text

Download Dynamic Memory Allocation and more Summaries Software Engineering in PDF only on Docsity!

C-LANGUAGE

LEARN THROUGH

QUESTIONS

PART- 15

DYNAMIC MEMORY

ALLOCATION

MALAY TRIPATHI
ASSISTANT PROFESSOR

Introduction

  • The allocation of memory at the run time (i.e. as a program executes)

is known as dynamic memory allocation. In C language, memory can

be dynamically allocated by calling malloc, calloc or realloc functions.

The prototypes of these functions are available in the header files

alloc.h or malloc.h.

Example - 1 - A program that illustrates the use of the malloc function

We can also use at place of header file

  • After Step 6 โ†’
  • After Step 7 โ†’
  • After Step 13 โ†’

calloc() function

  • The syntax of the calloc function is:

void* calloc(size_t n, size_t size);

  • The important points about the calloc function are as follows:
    • The calloc function allocates the memory space for an array of n

objects, each of whose size is specified by the parameter size.

  • All the bits in the allocated memory space are initialized to zero.
  • The function returns a void pointer (i.e. void*) to the allocated

memory space, if successful.

  • If the memory space cannot be allocated, a null pointer is returned.

Example- 2 A program that illustrates the use of the calloc function

realloc function()

  • The syntax of the realloc function is:

void* realloc(void *ptr, size_t size);

  • The important points about the realloc function are as follows:
    • The realloc function deallocates the old object pointed by ptr and returns a pointer to a new object that has the size specified by the parameter size.
    • The major use of the realloc function is to resize the dynamically allocated object (especially an array).
    • The content of the new object shall be the same as that of the old object prior to deal location, if the new object is greater in size than the old object. Any bytes in the new object beyond the size of the old object have undetermined values (i.e. garbage values).
  • Output of the example-
  • Example

Output of the example 3

Note โ†’ Since malloc, calloc and realloc functions return a void pointer, the returned pointer must be type casted to the appropriate type before use. โ†’ The lifetime of dynamically allocated objects extends from the time of their allocation until deallocation. The deallocation is automatically done with the termination of a program or can be done by giving a call to the function free.

Memory Leak

  • Memory leak is a common situation that happens when the dynamic

memory allocation is used without proper care. It occurs when the

dynamically allocated memory is no longer needed but it is not freed.

If we continuously keep on allocating the memory without freeing it

for reuse, the entire heap storage will be exhausted.

  • After this, it will not be possible to allocate any memory space from

the heap. In such circumstances, the memory allocation functions will

start failing and the program will start behaving unexpectedly.

Example 4 - A program that suffers from memory leak