That doesn't strike me as the sort of thing C++ would do, it would be considered inefficient by the designers. Unless I'm mistaken, you are just being lucky when creating your object.
This can be explained because each statement initialises using different parts of memory. The first creates and object on the heap and the second uses the stack. Generally, the stack is far more volatile than the heap because of the way programs execute (call - push scope onto stack, and return - pop off the stack).
An interesting experiment would to see what happens when you do the following:
| Code: |
int maxSize = 100;
int *arr = new int [maxSize];
/* Print original contents and change values */
for(int i = 0; i < maxSize; ++i)
{
std::cout << arr[i];
arr[i] = i;
std::cout << std::endl;
}
/* Delete the array (shouldn't zero memory) */
delete[] arr;
/* Initialise a new array on the heap with the same size */
int *arr2 = new int [maxSize ];
/* Print new array contents to see if they are the same as the values you wrote to the old array */
for(int i = 0; i < maxSize; ++i)
{
std::cout << arr2[i];
}
|
Of course, this may not work if the compiler is doing something funky with heap allocation. Just wondering what would happen... |