Interview Questions

You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()

C++ Interview Questions and Answers


(Continued from previous question...)

You have two pairs: new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()

Answer1
1.) “new and delete” are preprocessors while “malloc() and free()” are functions. [we dont use brackets will calling new or delete].
2.) no need of allocate the memory while using “new” but in “malloc()” we have to use “sizeof()”.
3.) “new” will initlize the new memory to 0 but “malloc()” gives random value in the new alloted memory location [better to use calloc()]

Answer2
new() allocates continous space for the object instace
malloc() allocates distributed space.
new() is castless, meaning that allocates memory for this specific type,
malloc(), calloc() allocate space for void * that is cated to the specific class type pointer.

(Continued on next question...)

Other Interview Questions