Dynamic Memory Management
Dynamic Memory Management
world
Dynamic Memory
Management
Go, change the
world
Memory Allocation
Two Types
•Static Memory Allocation
•Dynamic Memory Allocation
2
Go, change the
world
4
Go, change the
world
6
Go, change the
world
Better Solution
• Dynamic memory allocation
• Know how much memory is needed after the program is
run
• Example: ask the user to enter from keyboard
• Dynamically allocate only the amount of memory
needed
• C provides functions to dynamically allocate
memory
• malloc, calloc, realloc
• free
7
Go, change the
world
DYANMIC MEMORY
ALLOCATION
Memory is allocated before the execution of the Memory is allocated during the execution of the
program begins (During Compilation). program.
Variables remain permanently allocated. Allocated only when program unit is active.
In this type of allocation Memory cannot be In this type of allocation Memory can be
resized after the initial allocation. dynamically expanded and shrunk as necessary.
Functions
• malloc
• Allocates requested number of bytes and returns a
pointer to the first byte of the allocated space
• calloc
• Allocates space for an array of elements, initializes them
to zero and then returns a pointer to the memory.
• free
• Frees previously allocated space.
• realloc
• Modifies the size of previously allocated space.
10
Go, change the
malloc world
•The malloc function has only one argument, the size (in bytes) of
the memory block to be allocated.
•Note that size_t is a type, also defined in stdlib.h, which is used
to declare the sizes of memory objects
•should be careful while using the malloc function as the allocated
memory block is uninitialized, i. e., it contains garbage values.
malloc world
Example :
Dynamically allocate a memory block to store 50 integers as shown
below.
int *p; /* pointer to the memory block to be allocated */
malloc world
Int *p;
p= (int*) malloc(50 * sizeof(int)); /* alloc memory*/
if (p == NULL)
{
printf(“Error: Out of memory …\n”);
exit(1); /*Error code 1 is used here to indicate
out of memory situation */
}
•Once memory is allocated, this dynamically allocated array can be
used as a regular array. Thus, the ith element of array p in the
above example can be accessed as p [i].
Go, change the
calloc world
calloc world
Space: free
• An allocated block can be returned to the system for future use
by using the free function
• The deallocation actually returns that memory block to the
system heap.
• The prototype of free function is
void free(void * ptr)
will cause the program to give back the block to the heap (or free
memory). The argument ptr to free is any address that was
returned by a prior call to malloc.
•Deallocate the space pointed to by the pointer ptr
•Do nothing if ptr is NULL
•Note that no size needs to be mentioned for the allocated block,
the system remembers it for each pointer returned
16
crealloc Go, change the
world
•Size of dynamically allocated memory can be changed by using
realloc().
•The realloc (reallocate) function is used to adjust the size of a
dynamically allocated memory block.
• If required, the block is reallocated to a new location and the
existing contents are copied to it.
•Its prototype:
•void *realloc(void *ptr, size_t size);
•Example
int *p;
p = (int*)malloc(50 * sizeof(int));
p = (int*)realloc(p,100);
free(p)
Go, change the
world
Go, change the
Diffrence between malloc() and world
calloc()
• calloc() initializes the allocated memory with 0 value.
malloc() initializes the allocated memory with garbage
values.
• calloc :Number of arguments is 2
malloc: Number of argument is 1
• Syntax :
calloc :
(cast_type *)calloc(blocks , size_of_block);
malloc:
(cast_type *)malloc(Size_in_bytes);
20
Go, change the
Memory Leaks world
Example:
int A[n],i=0;
for (i=0;i<n;i++)
A[i] = random();
int* B = malloc(2*n);
B = A;
Go, change the
world
Example
int* A = malloc(4*sizeof(int));
int *B = A;
free(B);