Why this code does not cause memory leaks?
int iterCount = 1000;
int sizeBig = 100000;
for (int i = 0; i < iterCount; i++)
{
std::auto_ptr<char> buffer(new char[sizeBig]);
}
WinXP sp2, Compiler : BCB.05.03
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.
Because you’re (un)lucky.
auto_ptrcallsdelete, notdelete []. This is undefined behavior.Try doing something like this and see if you get as lucky:
The idea here is that your destructor for
Foowill not be called.The reason is something like this: When you say
delete[] p, the implementation ofdelete[]is suppose to go to each element in the array, call its destructor, then free the memory pointed to by p. Similarly,delete pis suppose to call the destructor on p, then free the memory.char‘s don’t have a destructor, so it’s just going to delete the memory pointed to by p. In my code above, it is not going to destruct each element in the array (because it’s not callingdelete[]), so some Foo’s will leave their local bar variable un-deleted.