I am planning on turning this code into a quick sort application, but I am using arrays, something I have not done since discovering vectors and becoming very spoiled with them. This leads me to this code.
int main() {
int numbers[] = { 6, 5, 1, 3, 8, 4, 7, 9, 2};
int* pivot; // will be used later when I implement quicksorting
int currentElement = numbers[0]; //will be used for quicksorting
std::cout<< "\n" << currentElement << "\n" << std::endl;
int *ptr = numbers;
for(int x = 0; x < sizeof(numbers); x++)
{
std::cout << "value at index: - " << x << " is: " << *(ptr + x) << std::endl;
}
return 0;
}
I am receiving this output.
5
value at index: - 0 is: 6
value at index: - 1 is: 5
value at index: - 2 is: 1
value at index: - 3 is: 3
value at index: - 4 is: 8
value at index: - 5 is: 4
value at index: - 6 is: 7
value at index: - 7 is: 9
value at index: - 8 is: 2
value at index: - 9 is: -858993460
value at index: - 10 is: 1211635769
value at index: - 11 is: 5241484
value at index: - 12 is: 12658446
value at index: - 13 is: 1
value at index: - 14 is: 6877496
value at index: - 15 is: 6875200
value at index: - 16 is: 5241572
value at index: - 17 is: 12658033
value at index: - 18 is: 1211635877
value at index: - 19 is: 12652609
value at index: - 20 is: 12652609
value at index: - 21 is: 4116480
value at index: - 22 is: 0
value at index: - 23 is: 0
value at index: - 24 is: 0
value at index: - 25 is: 0
value at index: - 26 is: 0
value at index: - 27 is: 0
value at index: - 28 is: 0
value at index: - 29 is: 12694900
value at index: - 30 is: 12694912
value at index: - 31 is: 0
value at index: - 32 is: 5241492
value at index: - 33 is: 0
value at index: - 34 is: 5241664
value at index: - 35 is: 12665872
Process finished with exit code 0
The main question I have is why am I getting values in indexes past index 8
? (value 2
)
How do those even exist? Is this because the declaration I made at int numbers[]
does not have a specified array size? The reason why I have that in the first place is I intend to populate the array with a random number generator with my quick sort algorithm, essentially treating the array like a vector. I know that this is illegal, but I am simply trying to relearn arrays, so I can be comfortable in other languages with them, as vectors do not exist in many others.
Am I getting these phantom indexes because the array allocation has a default block of memory that it takes if an array size is not specified, or is that already implicitly defined with the manual population of the array that I did with the values supplied in the curly braces and maybe that is causing the pointer to continue?