Difference b/w dynamic memory allocation in C and C++

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++

  1. In C, we have four functions malloc(), calloc(), realloc(),and free() to do dynamic memory allocation
  2. In C++, we have operators called new and delete operator
  3. C++ does the same job as malloc() and free() of C  with new and delete operator
  4. In C we need a header file called <stdlib.h> to allocate memory dynamically
  5. 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
  6. In C, all the dynamic memory allocation functions return void pointers for this reason, we need to do explicit typecasting in C
  7. 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

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.