1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Difference Between malloc/free & new/delete

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

  1. Trinity

    Trinity New Member

    Joined:
    Nov 23, 2011
    Messages:
    28
    Likes Received:
    11
    Trophy Points:
    0
    Occupation:
    Software Engineer
    In C, memory allocations on heap are done using following three library methods
    Code:
           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
    Code:
     malloc() 
    The corresponding free-ing the memory method is
    Code:
    void free(void *ptr); 
    Talking about C++, although all C dynamic memory allocation methods work, still it offers a different set which is
    Code:
     new 
    for dynamically allocating the memory and
    Code:
    delete
    
    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
    Code:
    #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");
        }
        else
        {
            strncpy(sptr, STRING, len+1);
            printf("String is %s\n", sptr);
        }
    
        if (sptr)
            free(sptr);
        sptr = NULL;
        
        return 0;
    }
    
    The Output:
    Code:
    String is myLinuxBook
    
    Alongwith here is an example illustrating where new/delete can be used.
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define MAX 30
    #define DEFAULT "Standard"
    
    using namespace std;
    
    class MyNode
    {
        private:
         int data;
         char name[MAX];
    
        public:
         MyNode()
         {
             data = 0;
             strncpy(name, DEFAULT, strlen(DEFAULT));
             printf("In Consructor\n");
         }
    
         ~MyNode()
         {
             printf("In Destructor\n");
         }
    
         void display()
         {
             printf("Name = %s   Data = %d\n", name, data);
         }
    
    };
    
    int main()
    {
        MyNode *node = new MyNode();
        node->display();
        delete node;
        return 0;
    }
    
    The Output
    Code:
    In Constructor
    Name = Standard   Data = 0
    In Destructor
    

    Caveat



    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