I've written a short code to get an idea about C-style arrays created by raw pointers. I know its unusual to use this method today, but I just want to try it out. So here is my code :
#include <iostream>
int main() {
int *ptr = new int[5];
for(unsigned int counter = 0; counter < 5; counter++) {
ptr[counter] = counter * counter;
std::cout << ptr[counter] << std::endl;
}
delete [] ptr;
std::cout << ptr[2] << std::endl; // Contains still a value
}
Output :
0
1
4
9
16
4
Its a very simple code, I've just created a memory field of five "int" characters and fill these with some numbers. I delete the array-pointer construct after usage to avoid memory leaks, but to my surprise contains the third to the fith position still values, the first and the second pointer were deleted correctly. Does anyone have an answer for this phenomenon ?