0

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?

anothermh
  • 9,815
  • 3
  • 33
  • 52
Zert
  • 11
  • 2
  • 1
    *I am hoping this is not a duplicated question* Duplicated questions doesn't cause any harm, they add more pointers to a common problem. – Winter Jul 20 '18 at 23:32
  • 7
    `sizeof(numbers)` gives the size of the array in bytes, not the number of elements in the array. And once you read past the end of an array, all bet are off. – Beta Jul 20 '18 at 23:34
  • 1
    Beta is correct. The number of items is determined with `sizeof(numbers) / sizeof(numbers[0])`. That's a common error, unfortunately. Even the best will do it once in a while. – Alexis Wilke Jul 20 '18 at 23:38
  • 1
    "Is this because the declaration I made at int numbers[] does not have a specified array size?". No. You are safe here. `int numbers[] = { 6, 5, 1, 3, 8, 4, 7, 9, 2};` sizes itself appropriately for the number of element in the initializer and knows how big it is. You could use a range-based `for` loop here (`for(auto x: numbers) std::cout << x << std::endl;`)and avoid the headache. – user4581301 Jul 20 '18 at 23:39
  • I should also mention that my very first programming language was Java, which had arrays that did not allow me to go out of bounds like this one does. I was aware that C++ allows this, but I did not know to what extent, now I do I suppose, but based on the link [link]https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why Should I be instead just sticking with vectors in C++ simply because they are dynamic in this situation? – Zert Jul 20 '18 at 23:46
  • 1
    You can go out of bounds on a `vector` as well. `operator[]` is unchecked, but the `at` method will throw an exception. Also look into `std::array` which has most of the `vector` gooddies, but isn't resizable. Depending on your insertion and removal pattern `std::deque` is often handy. C++ gives a lot of container options, so familiarize yourself with them and pick the one that best fits the task at hand. – user4581301 Jul 21 '18 at 00:06

0 Answers0