A class template (not to be confused with template class template) defines the amount, type, and positioning of the members. The class template doesn't allocate memory, until it's instantiated. As
Athar said, the compiler is free to insert padding bytes wherever it wants. A decent compiler will add bytes where it makes sense. Adding bytes is called
Alignment, and its purpose is to align all the members on word-sized boundaries so that the CPU can cache the members more efficiently. A compiler will only insert bytes only if the class template isn't perfectly aligned (the size is not a power of 2).
Passing the class template to
sizeof will yield the size of each member, including the additional bytes.
When you instance a class template, the object can go in 1 of 2 places: heap, or stack. By default, it'll be placed on the stack with automatic storage (automatic destructor call).
Raman009 wrote: |
---|
"Does it mean that it completely depends on the compiler to decide any number of padding bytes and allocate the same to the class ?" |
Yes, it's compiler specific. However, compilers will align data members according to the architecture of the host CPU.
Wazzak