In what cases do I use malloc and/or new?

前端 未结 19 1616
北恋
北恋 2020-11-21 17:41

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the

相关标签:
19条回答
  • 2020-11-21 18:08

    In the following scenario, we can't use new since it calls constructor.

    class  B  {
    private:
        B *ptr;
        int x;
    public:
        B(int n)  {
            cout<<"B: ctr"<<endl;
            //ptr = new B;  //keep calling ctr, result is segmentation fault
            ptr = (B *)malloc(sizeof(B));
            x = n;
            ptr->x = n + 10;
        }
        ~B()  {
            //delete ptr;
            free(ptr);
            cout<<"B: dtr"<<endl;
        }
    };
    
    0 讨论(0)
  • 2020-11-21 18:12

    Unless you are forced to use C, you should never use malloc. Always use new.

    If you need a big chunk of data just do something like:

    char *pBuffer = new char[1024];
    

    Be careful though this is not correct:

    //This is incorrect - may delete only one element, may corrupt the heap, or worse...
    delete pBuffer;
    

    Instead you should do this when deleting an array of data:

    //This deletes all items in the array
    delete[] pBuffer;
    

    The new keyword is the C++ way of doing it, and it will ensure that your type will have its constructor called. The new keyword is also more type-safe whereas malloc is not type-safe at all.

    The only way I could think that would be beneficial to use malloc would be if you needed to change the size of your buffer of data. The new keyword does not have an analogous way like realloc. The realloc function might be able to extend the size of a chunk of memory for you more efficiently.

    It is worth mentioning that you cannot mix new/free and malloc/delete.

    Note: Some answers in this question are invalid.

    int* p_scalar = new int(5);  // Does not create 5 elements, but initializes to 5
    int* p_array  = new int[5];  // Creates 5 elements
    
    0 讨论(0)
  • 2020-11-21 18:12

    The new and delete operators can operate on classes and structures, whereas malloc and free only work with blocks of memory that need to be cast.

    Using new/delete will help to improve your code as you will not need to cast allocated memory to the required data structure.

    0 讨论(0)
  • 2020-11-21 18:13

    Always use new in C++. If you need a block of untyped memory, you can use operator new directly:

    void *p = operator new(size);
       ...
    operator delete(p);
    
    0 讨论(0)
  • 2020-11-21 18:14

    To answer your question, you should know the difference between malloc and new. The difference is simple:

    malloc allocates memory, while new allocates memory AND calls the constructor of the object you're allocating memory for.

    So, unless you're restricted to C, you should never use malloc, especially when dealing with C++ objects. That would be a recipe for breaking your program.

    Also the difference between free and delete is quite the same. The difference is that delete will call the destructor of your object in addition to freeing memory.

    0 讨论(0)
  • 2020-11-21 18:15

    If you are using C++, try to use new/delete instead of malloc/calloc as they are operators. For malloc/calloc, you need to include another header. Don't mix two different languages in the same code. Their work is similar in every manner, both allocates memory dynamically from heap segment in hash table.

    0 讨论(0)
提交回复
热议问题