Hey all, just wondering whether the following would cause a memory leak?
char* a = "abcd"
char* b = new char[80];
strcpy(b, a);
delete[] b;
Will it delete the whole block of 80 or just the 4 characters copied into it by strcpy? Thanks!
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 allocated 80 bytes into
b, sodelete[]will free 80 bytes. What you did with the array in the meantime is irrelevant.(Unless, of course, you corrupted the heap, in which case
delete[]will probably crash.)EDIT: As others have pointed out, since
bis an array, you need to usedelete[] b;instead ofdelete b;. Some implementations might let you get away with that, but others won’t, and it would still be wrong.