Difference Between malloc/free & new/delete

Discussion in 'C++' started by Trinity, Oct 29, 2012.

  1. Trinity

    Trinity New Member

    Nov 23, 2011
    Likes Received:
    Trophy Points:
    Software Engineer
    In C, memory allocations on heap are done using following three library methods
           void *malloc(size_t size);
           void *calloc(size_t nmemb, size_t size);
           void *realloc(void *ptr, size_t size);
    Though, most commonly used is
    The corresponding free-ing the memory method is
    void free(void *ptr); 
    Talking about C++, although all C dynamic memory allocation methods work, still it offers a different set which is
    for dynamically allocating the memory and
    for free-ing up the memory. Both of these are operator and not methods. though.

    The Difference Between malloc/free and new/delete

    Even if we talk about C++ programming, malloc/free are widely used for allocating memory dynamically, besides new/delete. It is because, both vary in the way they do their job of memory allocation, and are efficient to use in their own specific case. Some major differences between the two being:
    • As already stated, malloc/free are library methods, whereas new/delete are C++ operators
    • malloc/free just returns the pointer to allocated memory chunk, whereas new/delete allocates and calls the constructor/destructor for the particular object.
    • In case of malloc/free, the return pointer needs to be typecasted, but no such typecasted required in case if new/delete
    • Interestingly, new/delete operators can be overloaded by the user (a C++ concept), however malloc/free methods cannot be overridden.
    Considering the above points, malloc/free are well suited in cases involving C-structs, basic data types, however where objects of classes are involved, new/delete operators are better suited. Howsoever, these is not a hard and fast rule, and can have variations depending upon certain things as per the programming requirements.

    Implementation Examples

    Here is an example showcasing malloc/free in a simple C program
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define STRING "myLinuxBook"
    int main()
        int len; 
        char *sptr = NULL; 
        len = strlen(STRING);
        sptr = (char*) malloc (len *sizeof(char)); //dynamic memory allocation
        if (sptr == NULL)
             printf("Error in  memory allocation\n");
            strncpy(sptr, STRING, len+1);
            printf("String is %s\n", sptr);
        if (sptr)
        sptr = NULL;
        return 0;
    The Output:
    String is myLinuxBook
    Alongwith here is an example illustrating where new/delete can be used.
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define MAX 30
    #define DEFAULT "Standard"
    using namespace std;
    class MyNode
         int data;
         char name[MAX];
             data = 0;
             strncpy(name, DEFAULT, strlen(DEFAULT));
             printf("In Consructor\n");
             printf("In Destructor\n");
         void display()
             printf("Name = %s   Data = %d\n", name, data);
    int main()
        MyNode *node = new MyNode();
        delete node;
        return 0;
    The Output
    In Constructor
    Name = Standard   Data = 0
    In Destructor


    Very important point, one cannot use malloc() to allocate memory and delete to free the memory. Similarly, allocating using new and freeing using free() is incorrect. The reason being, the implementations of malloc() and operator delete which uses some metadata values being stored in some specific memory pool to determine which memory address and what size of memory to free. Hence, only free() implementation knows how to understand the metatdata by malloc() and only delete understands the one by new.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice