Bradley Justice
Bradley Justice

Reputation: 1

Weird and Improper Array size behavior in C++ when initialized in a class

I am trying to create a class called Player which has an array called scores, declared with a size in the Player class properties. Yet, when I initialize a new player, the sizeof(scores) method is giving me 20, and, does, in fact, initialize a score array of size 20 as opposed to 5. I am wondering why and how to fix this. Here is the code and output.

    #include <iostream>
    #include <iomanip>
    #include <sstream>

    using namespace std;

    struct stats {
        unsigned int min;
        unsigned int max;
        unsigned int mode;
        unsigned int modeFreq;
        unsigned int mean;
        unsigned int total;
    };

    class Player {
        public:
            int scores[5];
            stats pStats;
            void inputScores();
            void calculateStats();
            void printScores();
    };

    void Player::printScores(){
        cout << "Printing Scores..." << endl;
        for (int i = 0; i < sizeof(scores); i++) {
            cout << scores[i] << endl;
        }
    }

    void Player::inputScores(){
        for (int i = 0; i < sizeof(scores); i++) {
            cout << "Enter a score for [" << i << "]: ";
            int input;
            cin >> input;
            scores[i] = input;
        }
    }

    int main() {
        Player p;
        cout << sizeof(p.scores);
        p.inputScores();
        p.printScores();
    }

Which gives me this:

    20
    Enter a score for [0]: 1
    Enter a score for [1]: 2
    Enter a score for [2]: 3
    Enter a score for [3]: 4
    Enter a score for [4]: 5
    Enter a score for [5]: 6
    Enter a score for [6]: 7
    Enter a score for [7]: 8
    Enter a score for [8]: 20
    Enter a score for [0]: 1
    Enter a score for [1]: 2
    Enter a score for [2]: 3
    Enter a score for [3]: 4
    Enter a score for [4]: 5
    Enter a score for [5]: 6
    Enter a score for [6]: 7
    Enter a score for [7]: 8
    Enter a score for [8]: 
    ....

So on, up to 20...

Upvotes: 0

Views: 87

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

sizeof(scores) is byte-size of scores object, which is 5 * sizeof(int). On popular platforms that would produce 20. So, you are iterating from 0 to 19 and accessing array elements that do not exist.

The strange output that you observe is nothing more than just a manifestation of undefined behavior caused by out-of-bounds array access.

So, why are you trying to use sizeof(scores) as array size?

There's a sizeof-based technique that can determine array size, but it involves division of byte-size of the entire array by the byte-size of a single element

sizeof scores / sizeof *scores

And keep in mind that it only works for objects of array type (not for pointers).

Alternatively, in modern C++ you can use

std::extent<decltype(scores)>::value

to determine array size.

Upvotes: 3

Related Questions