Dynamic memory allocation means we allocate memory to the variables at runtime but in reality, we have little difference when allocating memory dynamically in C and C++
- In C, we have four functions malloc(), calloc(), realloc(),and free() to do dynamic memory allocation
- In C++, we have operators called new and delete operator
- C++ does the same job as malloc() and free() of C with new and delete operator
- In C we need a header file called <stdlib.h> to allocate memory dynamically
- In C++, new is an operator, not a function that’s why we do not need any header file to include during, dynamic memory allocation
- In C, all the dynamic memory allocation functions return void pointers for this reason, we need to do explicit typecasting in C
- For dynamic memory allocation in C++, the new operator doesn’t require explicit typecasting because implicit typecasting has occurred in a new operator
To conclude, in both C/C++ for dynamic memory allocation we require pointers to allocate memory in the heap area
Leave a Reply