I have following line of code.
Code Sample 1
char * arr = new char[10];
arr++;
delete arr;
Code Sample 2
char * arr = new char[10];
delete arr;
I have two sample codes. Code sample one is crashing at delete while code sample 2 works okay. There is an only difference of arr++. What exactly happens in these two code samples. Can anybody explain?
deletemust be the result of anewexpression, otherwise the behaviour is undefined.arris initialised with anewexpression. After incrementing, its value is no longer the result of anewexpression. Hence the behaviour of your code is undefined.std::vectorhas support for adding and removing individual elements.delete[] arr;.