If I can create a QString in C++ like:
QString s = "my string"; // or,
QString *s = new QString("my string");
Then, when will I event need to use malloc?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You never need to use
mallocin C++.Ok, now that I’ve said never, one exception is when you are using C code that for some reason or another takes ownership of a block of memory you give it and later calls
freeon a pointer to that memory to deallocate it.I have never seen that before (I don’t usually use C libraries and I don’t know how common that scenario is), it’s just a contrived situation that I can think of where using
mallocwould not be optional, because it’s undefined behaviour to callfreeon a piece of memory created bynew.