KEMBAR78
Dynamic Memory Allocation-2 | PDF | Pointer (Computer Programming) | Software Development
0% found this document useful (0 votes)
5 views16 pages

Dynamic Memory Allocation-2

The document explains dynamic memory allocation in C and C++, contrasting it with static memory allocation. It details functions like malloc, calloc, realloc, and free, highlighting their usage, differences, and memory management practices. Additionally, it compares malloc and calloc, emphasizing their initialization behavior and performance characteristics.

Uploaded by

Bitcoin Blast
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)
5 views16 pages

Dynamic Memory Allocation-2

The document explains dynamic memory allocation in C and C++, contrasting it with static memory allocation. It details functions like malloc, calloc, realloc, and free, highlighting their usage, differences, and memory management practices. Additionally, it compares malloc and calloc, emphasizing their initialization behavior and performance characteristics.

Uploaded by

Bitcoin Blast
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/ 16

Dynamic Memory Allocation

Introduction

In the C and C++ programming languages there are generally two


different types of memory allocation that can be used to hold the
contents of variables.
1) Static memory allocations
Fixed location, fixed size.
2) Dynamic memory allocations
Variable location, variable size.

2
Static Memory Allocation

Till now what we have done for memory allocation to store data
is called static memory allocation.
Ex: int i;
static int j;
int a[10];
sizes of these allocations are fixed at the time of compilation.

3
Dynamic memory allocation


The sizes, addresses and contents of such memory vary at run-
time.

These memory allocations are called dynamic memory
allocations because their location and size can vary throughout
the lifetime of a program.

Such memory allocations are placed in a system memory area
called the heap

4
Memory Allocation layout

5
Dynamic memory allocation functions

Dynamic memory allocation related all predefined functions are


declared in <stdlib.h> header file. They are,
#include <stdlib.h>
void *malloc(size_t size);
void *calloc(size_t nmemb, size_t size); //for allocation
void *realloc(void *ptr, size_t size);
void free(void *ptr); // for de-allocation

6
Explanation : malloc()

It allocates requested number of bytes and returns a pointer
to the first byte of the allocated space.

The memory is not initialized

If size is 0, then malloc() returns either NULL, or a unique
pointer value that can later be successfully passed to free().
int *ptr;
ptr=(int*)malloc(sizeof (int));
//4 byte is allocated

7
Explanation : malloc() (Contd...)


malloc() always allocates a block of contiguous bytes. The
following program allocates memory dynamically, and
accesses it using pointer.

The allocation can fail if sufficient contiguous memory space is
not available. If it fails, malloc() returns NULL.

if ((p = (int *) malloc(100 * sizeof(int))) == NULL)


{
printf (“\n Memory cannot be allocated”);
return;
}

8
Explanation : calloc()

It allocates space for an array of elements, initializes them to
zero and then returns a pointer to the memory.

The memory is set to zero

If nmemb or size is 0, then calloc() returns either NULL, or a
unique pointer value that can later be successfully passed
to free().
int*arr;
arr=(int*)calloc(10, sizeof(int));
// array of 10 elements each of size 4bytes

9
Explanation : free()


When you no longer need the data stored in a block of
memory, you may release the block for future use.

The free() function frees the memory space pointed to by ptr
For example:
free(arr);
free(str);
free(p);

10
Explanation : realloc()


The life of dynamically allocated memory in heap section starts
when it is allocated by programmer using either malloc, calloc
or realloc and ends if free is called or program is terminated.

The realloc() function changes the size of the memory block
pointed to by ptr to size bytes.

The contents will be unchanged in the range from the start of
the region up to the minimum of the old and new sizes.

If the new size is larger than the old size, the added memory
will not be initialized.

11
Malloc and realloc examples

ptr = malloc (size);


then reallocation of space may be done as:
ptr = realloc (ptr, newsize);
For example:
int*arr;
arr=(int*)malloc(sizeof(int)*5); //20 byte
arr=(int*)realloc(arr,40); //40 bytes

12
Malloc and realloc examples (Contd...)

13
Malloc and realloc examples (Contd...)

The new memory block may or may not begin at the same place
as the old one.
If it does not find space, it will create it in an entirely different
region and move the contents of the old block into the new block
The function guarantees that the old data remains intact.
If it is unable to allocate, it returns NULL and frees the original
block.

14
Malloc Vs Calloc
malloc() calloc()
The name malloc stands The name calloc stands
for memory allocation. for contiguous allocation.

void *malloc(size_t n) returns a void *calloc(size_t n, size_t size)


pointer to n bytes of uninitialized returns a pointer to enough free
storage, or NULL if the request space for an array of n objects of
cannot be satisfied. the specified size, or NULL if the
request cannot be satisfied.

calloc() take two arguments those


malloc() takes one argument that are: number of blocks and size of
is, number of bytes. each block.

15
Malloc Vs Calloc (Contd...)
malloc() calloc()
syntax of malloc(): syntax of calloc():
void *malloc(size_t n); void *calloc(size_t n, size_t size);
The allocated region is initialized to zero.
It returns pointer to
uninitialized storage
calloc takes little longer than malloc
because of the extra step of initializing the
allocated memory by zero. However, in
malloc is faster
practice the difference in speed is very
than calloc but not safer. tiny and not recognizable.

For example,
For example,
malloc(10*sizeof(int));
calloc(10,sizeof(int));
allocates 40 bytes of allocates enough memory space to store
memory block. array of 10 integers each of size 4 bytes
contagiously.

16

You might also like