You may have seen the message system...
not for the faint-of-heart, but very instructive: A 3-dimensional, dynamically allocated array.
The dimensions are state (type , source , severity). We allocate the array dynamically in the class constructor because we don’t want to hard-wire the respective numbers.
class filter_msg_buffer : public msg_buffer {
streambuf * original_streambuf;
// allocate new memory for the matrix
state = new int **[msg_type_max];
for (i=0; i< msg_type_max; i++)
state[i] = new int* [msg_source_max];
for ( j =0; j<msg_source_max; j++)
state[i][j] = new int [msg_sev_max];
for (i=0; i < msg_type_max; i++)
for (j=0; j < msg_source_max ; j++)
for (k=0; k < msg_sev_max ; k++)
please convince yourself that we end up with an appropriately-sized array, and that we free the memory again correctly.
// free memory from the matrix
for (i=0; i< msg_type_max; i++)
for ( j =0; j<msg_source_max; j++)