Tell me what the 3rd line is doing please.
int main(){
int *p = new int[3];
*p++=0; // What's this line doing?
delete p;
return 0;
}
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.
*p++=0;means this:sizeof(int)zero bytes to an address stored inp.pbysizeof(int).In other words — you have incremented the pointer and what you then passing to
deleteis not the same as was returned by operatornew[].As @FredLarson has also mentioned, you have to use
delete [] p;in order to delete an array.Also, I’d recommend you read up on pointers, pointer arithmetics and pre-/post-increment. Pick a book from our Definitive C++ Book Guide and List.