I need a dinamically allocated bi-dimensional array of objects.
I declared a static pointer to pointer to object as follows:
server_session **server_session::Pglvcl_sess;
There's a method to populate dinamically the array of array of object:
int server_session::createSession()
{
int ret = -1;
// looks for the next available position on the array
for (int i = 0; i <= MAX_SESSION; i++)
{
// if there's an available position, instantiates an object
if (server_session::Pglvcl_sess[i] == NULL)
{
// instantiates object
server_session::Pglvcl_sess[i] = new server_session();
ret = i;
break;
}
}
return ret;
}
Should I malloc
the server_session::Pglvcl_sess
variable?
What is the proper way to initialize it?
EDIT:
The application executes this method at startup, is it OK?
void server_session::initializeSessions()
{
server_session::Pglvcl_sess = ( server_session * * ) malloc(MAX_SESSION * sizeof(server_session));
for (int i = 0; i <= MAX_SESSION; i++)
{
if (server_session::Pglvcl_sess[i] != NULL)
{
server_session::Pglvcl_sess[i] = NULL;
}
}
}