Skip to main content
added 10 characters in body
Source Link
TheCatWhisperer
  • 5.3k
  • 1
  • 24
  • 43

If you are looking for a standard approach, I cannot help you, but this is not particularly difficult to roll yourself if all the objects are the same size.

int maxNumberOfObjs = x;
int objectSize = y;
int count = 0;

void* ptr = malloc(maxNumberOfObjs * objectSize); //don't forget to free this or you will get a mem leak

//Add object
MyObj* myObjPtr = new (ptr + count * objectSize) MyObj();
++count;

//Iterate over your objects
int i = 0;
while (i < count)
{
    MyObj* obj = ptr + i * objectSize;
    ++i;
}

Been a while since I have done C++, so you may have to do a few tweaks and casts, but you get the idea I hope

If you are looking for a standard approach, I cannot help you, but this is not particularly difficult to roll yourself if all the objects are the same size.

int maxNumberOfObjs = x;
int objectSize = y;
int count = 0;

void* ptr = malloc(maxNumberOfObjs * objectSize); //don't forget to free this or you will get a mem leak

//Add object
MyObj* myObjPtr = new (ptr + count * objectSize) MyObj();
++count;

//Iterate over your objects
int i = 0;
while (i < count)
{
    MyObj* obj = ptr + i * objectSize;
    ++i;
}

Been a while since I have done C++, so you may have to do a few tweaks, but you get the idea I hope

If you are looking for a standard approach, I cannot help you, but this is not particularly difficult to roll yourself if all the objects are the same size.

int maxNumberOfObjs = x;
int objectSize = y;
int count = 0;

void* ptr = malloc(maxNumberOfObjs * objectSize); //don't forget to free this or you will get a mem leak

//Add object
MyObj* myObjPtr = new (ptr + count * objectSize) MyObj();
++count;

//Iterate over your objects
int i = 0;
while (i < count)
{
    MyObj* obj = ptr + i * objectSize;
    ++i;
}

Been a while since I have done C++, so you may have to do a few tweaks and casts, but you get the idea I hope

Source Link
TheCatWhisperer
  • 5.3k
  • 1
  • 24
  • 43

If you are looking for a standard approach, I cannot help you, but this is not particularly difficult to roll yourself if all the objects are the same size.

int maxNumberOfObjs = x;
int objectSize = y;
int count = 0;

void* ptr = malloc(maxNumberOfObjs * objectSize); //don't forget to free this or you will get a mem leak

//Add object
MyObj* myObjPtr = new (ptr + count * objectSize) MyObj();
++count;

//Iterate over your objects
int i = 0;
while (i < count)
{
    MyObj* obj = ptr + i * objectSize;
    ++i;
}

Been a while since I have done C++, so you may have to do a few tweaks, but you get the idea I hope