Question

Does doing something like this use dynamic memory allocation?

template <class T, int _size> 
class CArray 
{ 
public:
    ... 
private:
    T m_data[_size]; 
};

Can someone explain to me what's going on behind the scenes when I create the object?

CArray<SomeObject, 32> myStupidArray;
Was it helpful?

Solution

As mentioned in the other answers, templates are evaluated at compile time. If you're interested you can have g++ spit out the class hierarchy where you can verify its size:

template <class T, int _size>
class CArray
{
public:
private:
  T m_data[_size];
};

int main(int argc, char **argv) {
  CArray<int, 32> myStupidArray1;
  CArray<int, 8> myStupidArray2;
  CArray<int, 64> myStupidArray3;
  CArray<int, 1000> myStupidArray4;
}

Compile with -fdump-class-hierarchy:

g++ -fdump-class-hierarchy blah.cc

There should be a file named blah.cc.t01.class in the current directory:

Class CArray<int, 32>
   size=128 align=4
   base size=128 base align=4
CArray<int, 32> (0x40be0d80) 0

Class CArray<int, 8>
   size=32 align=4
   base size=32 base align=4
CArray<int, 8> (0x40be0e80) 0

Class CArray<int, 64>
   size=256 align=4
   base size=256 base align=4
CArray<int, 64> (0x40be0f80) 0

Class CArray<int, 1000>
   size=4000 align=4
   base size=4000 base align=4
CArray<int, 1000> (0x40be6000) 0

OTHER TIPS

No, it will be allocated in-place (e.g. either on the stack, or as part of a containing object).

With templates the parameters are evaluated at compile-time so your code effectively becomes;

class CArray 
{ 
public:
    ... 
private:
    SomeObject m_data[32]; 
};

Templates are evaluated at compile time, so m_data[_size] is compiled as m_data[32].

No, it does not use dynamic memory allocation. The compiler knows how much memory needs to be allocated, it is decided at compile-time, not at run-time.

It does not make any sense to explain C++ templates here, they are explained pretty well at other pages, like in the C++ faq lite.

The size of the array is known at compile time, so no, it won't be using dynamic memory allocation here. If you declare the array in a function scope then it'll be put on the stack just like any other type of object.

When you define variable-sized objects like this, what's really happening is that the compiler is building a class definition 'to order'. You'll just be using a class definition that contains 'T m_data[32]' in it.

1- Templates are evaluated only at compile time. So this array will be of size 32

2- You can't create an array on the stack using a variable, it must be a constant that can be evaluated at compile time, else you can use new (or malloc)

Dynamic memory allocation typically occurs when you call the new operator (or one of its friends). This allocates a chunk of memory from a memory reserve commonly referred to as a heap. Remember that unlike static allocation, the compiler doesn't take care of free-ing the memory and so you have to do it manually using the delete operator (or a suitable version thereof).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow