Can anybody please explain the malloc function? I only know that it is used in dynamic memory allocation. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anybody please explain the malloc function? I only know that it is used in dynamic memory allocation.

13th Jul 2017, 6:01 PM
Inderjeet Sahu
Inderjeet Sahu - avatar
3 Answers
+ 2
Don't use malloc, it is from C. In cpp you need to use new/delete. new allocates dynamic memory for the object and calls its constructor. Returns a typesafe pointer to the allocated object. delete frees allocated memory. MyClass *mc = new MyClass; mc->myMethod (); delete mc; In modern cpp it's better to use smart pointer instead of manual deletion.
13th Jul 2017, 6:07 PM
soman
0
Thanks i'll use this first than reply u. Thanks buddy
13th Jul 2017, 6:10 PM
Inderjeet Sahu
Inderjeet Sahu - avatar
0
You're welcome :)
14th Jul 2017, 5:42 AM
soman