Difference between new and malloc?
1. When new is used, class constructor will be called but malloc don’t know anything about class so it just allocates chunk of bytes.
1. When new is used, class constructor will be called but malloc don’t know anything about class so it just allocates chunk of bytes.
2. One disadvantage of new is that there is no way to reallocate memory. With malloc you can use realloc to extend the amount of memory.
3. new returns pointer to the object type, but malloc returns void *
4. When malloc failed to allocate memeory it returns a NULL value, but if new fails to allocate memory it throws an exception.
5. new gurantees allocating memory equivalent to the size of the object or required size.
For example;
Obj* obj = (Obj*)malloc(sizeof(int*3)); // This is allowed syntax but it may not be satisfying the memory requirement of Obj
6. new is an operator can be overloaded.
0 Comments:
Post a Comment
<< Home