I have dynamically allocated memory for an array of 5 elements, then tried to print its elements to std::cout
, which should have left me with a fairly straightforward result. I got something else instead, and it left me with some questions.
My code:
#include <iostream>
int main()
{
int *array = new int[5];
int array_size = sizeof(array);
for (int index = 0; index < array_size; index++) {
std::cout << array[index] << "\n";
}
std::cout << "\nThe array is " << array_size << " elements long.";
return 0;
}
This is what this resulted in:
0
0
0
0
0
0
132049
0
Now, I understand this isn't how things are done, but such a result has left me with several questions.
- Why is the size of the array 8, and not 5? Originally, I thought that it's becase the memory according to powers of 2, but I have a feeling I'm wrong.
- What's with 132049?