Dynamic memory allocation involves reserving blocks of memory at runtime using functions like malloc(), calloc(), and realloc(). Memory is allocated on the heap, which grows as needed, unlike the stack which has a fixed size. Variables on the heap must be manually freed with free() to avoid memory leaks. Proper memory management is important for application performance and security.
3. Memory and Allocation
Memory: Memory is the processes by which information is encoded, stored and retrieved.
Encoding allow information that is from the outside world to reach our senses in the forms of
chemical and physical stimuli.
Allocation: An alloation is something that you set aside for use .for instance if you want to set
aside a certain amount of hard drive space for an application,you can allocate how much in the
settings
4. Memory Allocation
• The placement of blocks of information in a memory system is called memory allocation.
• To allocate memory it is necessary to keep information of available memory in the system. If
memory Management system finds sufficient free memory, it allocates only as much memory as
needed ,keeping the rest available to satisfy the future future request .
• If sufficient memory is not available, swapping of blocks is done.
5. Why memory management is required?
• To provide the memory space to enable several processes to execute concurrently
• To provide a satisfactory level of performance for users
• To protect processes from each other
• To enable sharing of memory space between processes
• To make addressing transparent
7. Stack Memory
• Variables created on the stack will go out of scope and automatically deallocate.
• Much faster to allocate in comparison to variables on the heap.
• Stores local data, return addresses, used for parameter passing
• Can have a stack overflow when too much of the stack is used. (recursion, very large
allocations)
• Data created on the stack can be used without pointers.
• You would use the stack if you know exactly how much data you need to allocate before
compile time and it is not too big.
• Usually has a maximum size already determined when your program starts.
8. Heap Memory
• The heap contains free blocks. New allocations on the heap (by malloc) are satisfied by
creating a suitable block from one of the free blocks.
• The size of the heap is set on application startup, but can grow as space is needed.
• Variables on the heap must be destroyed manually and never fall out of scope. The data is freed
with free
• Slower to allocate in comparison to variables on the stack.
• Used on demand to allocate a block of data for use by the program.
• Can have fragmentation when there are a lot of allocations and deallocations
• You would use the heap if you don’t know exactly how much data you will need at runtime or if
you need to allocate a lot of data.
• Responsible for memory leaks
9. Types of memory allocation
Static memory allocation:
• In static memory allocation, size of the memory may be required for the calculation that must be
define before loading and executing the program.
Dynamic memory allocation:
• In the Dynamic memory allocation , the memory is allocated to a variable or program at the run
time.
• The only way to access this dynamically allocated memory is through pointer.
• Dynamic memory allocation function:
Malloc()
Calloc()
Free()
Realloc()
10. Malloc()
• A block of memory may be allocated using the function malloc. The malloc function reserves a
block of memory of specified size and returns a pointer of type void. This means that we can
assign it to any type of pointer. It takes the following form:
ptr=(cast-type*)malloc(byte-size);
• ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory
with size byte-size.
Example:
x=(int*)malloc(100*sizeof(int));
• On successful execution of this statement a memory equivalent to 100 times the area of int
bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x
of type int.
11. Calloc()
• Calloc is another memory allocation function that is normally used to request multiple blocks of
storage each of the same size and then sets all bytes to zero. The general form of calloc is:
ptr=(cast-type*) calloc(n,elem-size);
• calloc() allocates contiguous space for n blocks each size of elements size bytes. All bytes are
initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not
enough space a null pointer is returned.
Example:
x=(struct student*)calloc(5,sizeof(struct student));
• Struct student having three member name, age and id number, the calloc allocate the memory to
whole data for 5 such records.
12. Memory errors
• Using memory that you have not initialized.
• Using memory that you do not own.
• Using more memory than you have allocated.
• Using faulty heap memory management.
• We must be sure that requested memory has been allocated succesfully before using the pointer
x this may be done as follows:
If(x==NULL)
{
printf(“available memory is not sufficient”);
exit(1);
}
13. Memory leak
• Memory leak occurs when programmers create a memory in heap and forget to delete it.
• Memory leaks are particularly serious issues for programs which definition never terminate.
Example:
/* Function with memory leak */
#include <stdlib.h>
void f()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
return; /* Return without freeing ptr*/
}
14. Free()
• Compile time storage of a variable is allocated and released by the system in accordance with
its storage class. With the dynamic runtime allocation, it is our responsibility to release the space
when it is not required.
free(ptr);
ptr is a pointer that has been created by using malloc or calloc
• To avoid memory leaks, memory allocated on heap should always be freed when no longer
needed.
/* Function without memory leak */
#include <stdlib.h>;
void f()
{
int ptr = (int ) malloc(sizeof(int));
/* Do some work */
free(ptr);
return;
}
15. Realloc()
• The memory allocated by using calloc or malloc might be insufficient or excess sometimes in
both the situations we can change the memory size already allocated with the help of the function
realloc. This process is called reallocation of memory. The general statement of reallocation of
memory is :
ptr=realloc(ptr,newsize);
Example:
#include <stdlib.h>;
void f()
{
str = (char *) malloc(15);
str = (char *) realloc(str, 25);
return;
}
16. Refrences
• Programming in ANSI C [E Balaguruswamy],4edition, ISBN:13 978-0-07-064822-7.
• http://www.geeksforgeeks.org/memory-layout-of-c-program/.
• http://www.tenouk.com/ModuleZ.html