Q:

Pointer initialization of a struct array

my application crashes when it comes to it.
So I have a struct like this for example(but in reality it has many more things)
struct Record
{
    float m_fSimulationTime;
    unsigned char m_szflags;
};

In my class I have it declared like this:
Record *m_record[64];

And then I initalize it: (and here the crash occoures (acces violation on read))
void ClassXYZ::initRecord()
{
    for (int i = 0; i <= 32; i++)
        for (int j = 0; j < 9; j++)
            m_record[i][j].m_fSimulationTime = 0.0f; // here happens the crash
}

I hope you can help me out what I'm missing here x.x
Thanks in advice!

A:

The variable m_record is an array of pointers. You need to initialize the pointers first before you access them.
For example:
for (int i = 0; i <= 32; i++)
{
    m_record[i] = new Record[9];  // Make the pointer actually point somewhere
    for (int j = 0; j < 9; j++)
        m_record[i][j].m_fSimulationTime = 0.0f;

}

If the size 9 is fixed at the time of compilation, a better solution would be to use an array of arrays:
Record m_record[64][9];

In this case I would rather recommend using std::array instead though.
If the size of either array is not know at compile-time, but input at run-time then use std::vector instead.

