0% found this document useful (0 votes)
31 views8 pages

Dynamic Memory Management

Uploaded by

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

Dynamic Memory Management

Uploaded by

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

Dynamic Memory

Management
Introduction

• Dynamic memory management in C allows you


to allocate and deallocate memory during the
program's runtime, as opposed to static memory
management where the size and structure of data
are known at compile time.

•This is particularly useful for handling data when


the size is not known beforehand or when you
need flexible memory usage.
sizeof operator
• The sizeof operator in C is used to determine the size
(in bytes) of a data type or a variable.

•It returns the size as an unsigned integer of type size_t,


which is defined in the standard header file <stddef.h>
and typically used when dealing with memory and
pointer arithmetic.

• Syntax :-
sizeof(type)
sizeof(variable)
Functions for Dynamic Memory Management

• The function for the dynamic memory management can be found in stdlib.h
library .They are as follows :

1. void *malloc(size_t size)


• The function malloc allocates storage for an object whose size is specified
by size.

• It returns a pointer to the allocated storage and NULL if it is not possible


to allocate the storage requested.

• The allocated storage is not initialized in any way.

• For ex:
float *fp , fa[10];
fp=(float *) malloc(sizeof(fa));

• This allocates the storage to hold an array of 10 floating-point elements


and assigns the pointer to this storage to fp.
2. void *calloc (size_t nobj, size_t size)

• The function calloc allocates the storage for an


array of nobj objects, each of size size. It returns
a pointer to the allocated storage and null if it is
not possible to allocate the storage requested.

• The allocated storage is initialized to zeros.


3. void *realloc (void *p, size__t size)
• The function realloc changes the size of the object
pointed to by p to size.

• It returns a pointer to the new storage and NULL if it is


not possible to resize the object, in which case the object
(*p) remains unchanged.

• The new size may be larger or smaller than the original


size.

• If the new size is larger, the original contents are


preserved and the remaining space is uninitialized; if
smaller, the contents are unchanged up to the new size.
4. free()
• The function free deallocates the storage
pointed to by p, where p is a pointer to the
storage previously allocated by malloc, calloc,
or realloc.

• If p is a null pointer, free does nothing.


Advantages & Disadvantages Of Dynamic Memory
Management

You might also like