Home > Software design >  C Initialise emtpy array based on template defined length and type
C Initialise emtpy array based on template defined length and type

Time:01-14

Good whatever part of the day you're reading this!

I've been trying to implement my own version of an std::deque-like container. I thought it would be possible to use templates to customise the size and type of the inner array 'blocks', but I can't seem to get the syntax right. Or at least I think what I'm doing is somehow possible.

template<typename T, int ChunkSize>
class ChunkList
{
public:
  ChunkList()
  {
    T first[ChunkSize] = new T[ChunkSize]; 
    // error: array must be initialized with a brace-enclosed initializer

    m_ChunkPointers.push_back(first);
  }

private:
  // store pointers to the chunks
  std::vector<T[]> m_ChunkPointers;
};

int main()
{
  ChunkList<int, 4> cl = new ChunkList<int, 4>();
}

// compiled with g   on a windows x64 machine

I found a Stack Overflow answer relating to the brace-enclosed initializer but I don't know how to initialize those with the template syntax.

The only other solution I can think is is using something like malloc(sizeof(T) * ChunkSize) and then casting that to an T[] or something.

Either way any help/advice would be greatly appreciated, always happy to learn new things!

PS: Even if there is a way more efficient solution I'd still like to know if there is a way to make templates behave the way I intend to here, or rather why this isn't valid.

CodePudding user response:

We have a few problems here.

1: In main you wrote ChunkList<int, 4> cl = new ChunkList<int, 4>(); This will not work because new will return a ChunkList* not a ChunkList you need to write ChunkList<int, 4>* cl = new ChunkList<int, 4>(); or ChunkList<int, 4> cl{}; depending on the hact if you need an object or a pointer to an object

2:std::vector<T[]> m_ChunkPointers; declares a vector of empty arrays. I think you menat to write std::vector<T*> m_ChunkPointers; storeing pointers (to the start of the arrays.)

3: T first[ChunkSize] = new T[ChunkSize]; needs to be T *first = new T[ChunkSize];

You mixed together the T x[N]; sintax and the T*x=new T[N]; syntax. the first will create an array on the stack the second will create one array, living until the end of the function on the heap, living until you detete it.

Note: Welcome to C . In C the variables the object themself, not referances/pointers. You need to manage the lifetimes of the objects living in the heep. If you don't want to delete every object you used I suggest to take a look on unique_ptr and shared_ptr

CodePudding user response:

You are already using std::vector why not a proper container for the chunks too?!?

template<typename T, int ChunkSize>
struct ChunkList {
  ChunkList() : m_Chunks(1) {}
private:
  std::vector<std::array<T,ChunkSize>> m_Chunks;
};

// store pointers to the chunks - You do not want to store pointers, you want to store arrays. You attempted to store them on the heap, but that complication is totally unnecessary, because they are of fixed size and std::vector does already manage its elements in dynamically allocated memory.

  • Related