Is the new operator guaranteed to allocate a continuous chunk of heap memory? I.e. is
objects=new Base[1024];
in terms of memory allocation the same as
objects=(Base*)malloc(1024*sizeof(base));
or can there be gaps?
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.
Yes, the memory will be continuous. In terms of allocation, it’s the same as the
mallocversion, but there are several differences (calls to constructor,newdoesn’t returnNULL,mallocdoesn’t throw exceptions, etc.`).Note that you can’t mix up
new[]withdeleteorfree, you have to usedelete[] objectsto free the memory.