Dynamic Memory Allocation in C

Dynamic Memory Allocation

How to allocate memory for variable(arrays/strings) during run time

malloc(), calloc(), realoc() and free()

Dynamic Memory Allocation in C

Why dynamic memory allocation?

Usually, so far, the arrays and strings we're using have fixed length (i.e. length is known a complie time)

Example

char str[10];//allocates memory for 10 chars
printf("Enter a string: ");
fgets(str, 11, stdin);
			

What if the user wants to enter a string more than 10 chars long or if the length is known only at run time?

Dynamic Memory Allocation in C

malloc()

malloc() is used to request additional memory from the operating system during program execution

Input is the numer of consecutive bytes to be allocated

Reurn value is a pointer to the beginning of the block of memory allocate or NULL if malloc fails.

To use malloc(), you must #include <stdlib.h>

Dynamic Memory Allocation in C

malloc()

char *str;//declare a pointer to char
str = malloc(10);// char contains the address of the beginning of that block.
			
Dynamic Memory Allocation in C

free()

The function free() returns memory to the momery pool. It "frees" up memory

Syntax: free(ptr)

Where ptr "points to" memory previously allocated by malloc() function

To use free(), you must #include <stdlib.h>

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation

Dynamic Memory Allocation in C

Dynamic Memory Allocation