views:

49

answers:

2

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;
    }
  }
}
+3  A: 

Any reason not to use

Vector<Vector<server_session> >

and let it do the dynamic allocation and management for you?

Michael Dorgan
@Michael Dorgan, I was having trouble with a library compiled in pure c. I had segmentation fault's using Vector calling the functions from this lib. I don't know if it's related, but using pointer to pointer it worked...
pcent
+1  A: 

In case you really want an array instead of a vector, use std::tr1::array (or std::array or boost::array, all the same).

Klaim