c++ - How can I delete dynamic array pointer correctly -
c++ - How can I delete dynamic array pointer correctly -
possible duplicate: c++ delete - deletes objects can still access data? why doesn't delete destroy anything?
i've created dynamic array, , added values to values of array that.
int *parr; parr = new int[10]; for(int i=0;i<10;i++){ parr[i] = i+2; } delete[] parr; // after deletion can find value of array items. cout << parr[5] << endl;
as see in code above , in lastly line, can output 5th element in array without problem . supposed array has been removed.
to show memory can used again, consider expansion of code:
int *parr; parr = new int[10]; for(int i=0;i<10;i++){ parr[i] = i+2; } delete[] parr; int *parr2; parr2 = new int[10]; for(int i=0;i<10;i++){ parr2[i] = (2*i)+2; } cout << parr[5] << endl;
that prints out 12
me. i.e. value parr2[5]
actually. or @ to the lowest degree should machine specific compiler & version, etc. others have pointed out undefined behaviour. illustration shows @ to the lowest degree 1 kind of undefined behaviour.
c++
Comments
Post a Comment