Type Here to Get Search Results !

ads

Dynamic memory allocation in 'C' language

Dynamic memory allocation in 'C' language

Dynamic Memory Allocation in C language

Dynamic Memory Allocation in C is the process of allocating memory during runtime, as opposed to during the compile-time. In C, static memory allocation occurs during the compilation process and involves reserving memory for variables that have a fixed size and scope. On the other hand, dynamic memory allocation allows you to allocate and de-allocate memory dynamically during the execution of the program.

In C, the standard library provides functions for dynamic memory allocation, such as malloc(), calloc(), realloc(), and free(). These functions allow you to request a block of memory of a specific size at runtime, and then release it when it is no longer needed.

malloc() function is used to allocate a block of memory of a specified size, and it returns a pointer to the first byte of the allocated memory. The calloc() function is similar to malloc(), but it also initializes the allocated memory to zero. The realloc() function is used to resize a previously allocated block of memory.

Once you are done using the dynamically allocated memory, you should free it using the free() function. Failing to free dynamically allocated memory can lead to memory leaks, which can cause your program to use up more and more memory until it crashes.

Dynamic memory allocation is useful when you need to create data structures that can grow or shrink at runtime, or when you don't know the required size of a data structure until runtime. However, it's important to be careful when using dynamic memory allocation, as it can be prone to errors such as buffer overflows, memory leaks, and fragmentation.


Why Dynamic allocation is Required?

Dynamic allocation is required in C when you need to allocate memory at runtime, rather than at compile-time. Here are some of the reasons why dynamic allocation may be required:

1.     Unknown data size: When the size of the data that needs to be stored is not known at compile-time, dynamic allocation can be used to allocate memory as required.

2.     Data structures that change size: If you need to create data structures that can grow or shrink during runtime, dynamic allocation is necessary. Examples of such data structures include linked lists, trees, and dynamic arrays.

3.     Memory reuse: Dynamic allocation allows you to reuse memory that has been allocated but is no longer in use. This can help to optimize memory usage and reduce memory fragmentation.

4.    Sharing data between processes: Dynamic allocation can be used to share memory between processes, allowing for communication and data sharing between different parts of a program.

5.     User input: Dynamic allocation can be used when accepting user input, as the size of the input may not be known at compile-time.


Syntax:

malloc():    stands for Memory Allocation

         Ptr = (dataype*)malloc(SIZE);

ex.         int *p;
              P = (int*)malloc(20);
or     
              P = (int*)malloc(n*size(int));

calloc():  stands for continuous allocation

          ptr = (dataype*)calloc(n, size);

            note: (n,size) means two arguments & Default value is zero

ex.          int *p;
              P = (int*)calloc(10,2);
or
               P = (int*)calloc(n, size(int));  

realloc():   Resize allocation

               ptr = (datatype*)realloc(ptr, new size);

   ex.         P = (int*)malloc(10)
                        ...................................
                   P =  (int*)realloc(p, 20);

free(): 

          free(ptr);
   
ex.    P = (int*)malloc(10);
             .............................
         free(P);

In summary, dynamic allocation is necessary when you need to allocate memory at runtime, when the size of the data is unknown, or when you need to create data structures that can change size. Dynamic allocation allows for efficient memory usage and can be used for sharing data between processes or accepting user input.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.