I’ve been using pointer = new type[size] for a while now and just recently discovered malloc.
Is there a technical difference between malloc and new? If so, what are the advantages to using one over the other?
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.
mallocis a function call, new in this case an expression.The difference is;
newwill allocate memory and construct all the elements of that array with the default constructor.mallocjust gives back a chunk of uninitialized memory.Further still,
::operator newwill throwstd::bad_allocor a new handler if one was registered.The standard library defines a new that accepts a further parameter
nothrowwhich returns a 0 pointer if the allocation fails.