I need a C++ refresher. Why does this gives a memory exception?
pear = new char[1024];
pear = "happy go lucky";
delete [] pear; // exception
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.
Memory for 1024 character is allocated from heap and
pearpoints to the start of it.pearnow points to the string literal which resides in the read-only segment and the previously allocated memory is leaked.You try to free the read-only string, which is a undefined behaviour manifested as a runtime exception.