I just want to know When and Why we must use the malloc and allocate sufficient memory.
Should I use in destination pointer or what?
I just want to know When and Why we must use the malloc and allocate sufficient memory.
Should I use in destination pointer or what?
In C++, the use of malloc/free is discouraged. You should use new/delete instead, which allocate a block of memory AND initialize it (default construction). Since C++11, even new/delete should be avoided and you should use smart pointers like std::unique_ptr instead. However, malloc might still be useful for raw buffers and memory pools, but only in large scale applications where each cycle counts. For normal cases like yours (I suppose), don't even think about it.
You shouldn't use malloc in C++. Use new/delete or new[]/delete[] instead or use a smart pointer like std::shared_ptr<T>().
malloc does not call an objects constructor and malloc must be undone with free, (which does not call an objects destructor). On top of that, malloc is not type-safe as it returns a void*. Unless you have good reason, stay away from malloc.