I wrote a template class for a circular buffer:
template <class T> class CRingBuffer { };
Some of the operations performed by this class depend on an accurate size estimate T. This seems to work fine if T- BYTE(i.e. sizeof(T) == 1, check). However, when I try to use the same class, where T- DWORD, for some reason it sizeof(T)evaluates to 16. The last time I checked, the double word is 4 bytes, not 16. Does anyone know why this happens? Thank.
ADDITIONAL INFORMATION
I cannot publish all the code because of its proprietary nature, but here is the class declaration and function definition:
template <class T> class CRingBuffer
{
#pragma pack( push , 1 )
typedef struct BUFFER_FLAGS_tag
{
T * pHead;
T * pTail;
BOOL blFull;
BOOL blEmpty;
BOOL blOverrun;
BOOL blUnderrun;
DWORD dwItemCount;
} BUFFER_FLAGS, *LPBUFFER_FLAGS;
#pragma pack( pop )
private:
T * m_pBuffer;
T * m_pStart;
T * m_pEnd;
BUFFER_FLAGS m_tFlags;
DWORD m_dwCapacity;
public:
CRingBuffer( DWORD items = DEFAULT_BUF_SIZE );
~CRingBuffer();
public:
DWORD Add( T * pItems, DWORD num = 1, LPDWORD pAdded = NULL );
DWORD Peek( T * pBuf, DWORD num = -1, DWORD offset = 0, LPDWORD pWritten = NULL );
DWORD Delete( DWORD num, LPDWORD pDeleted = NULL );
DWORD Remove( T * pBuf, DWORD num = 1, LPDWORD pRemoved = NULL );
void Flush( void );
DWORD GetItemCount( void );
BYTE GetErrorStatus( void );
private:
void IncrementHead( LPBUFFER_FLAGS pFlags = NULL );
void IncrementTail( LPBUFFER_FLAGS pFlags = NULL );
};
template <class T> void CRingBuffer<T>::IncrementHead( LPBUFFER_FLAGS pFlags )
{
ASSERT(this->m_pBuffer != NULL);
ASSERT(this->m_pStart != NULL);
ASSERT(this->m_pEnd != NULL);
ASSERT(this->m_tFlags.pHead != NULL);
ASSERT(this->m_tFlags.pTail != NULL);
pFlags = ( pFlags == NULL ) ? &(this->m_tFlags) : pFlags;
if ( pFlags->blOverrun == FALSE )
{
pFlags->pHead += sizeof(T);
pFlags->blUnderrun = FALSE;
if ( pFlags->pHead == this->m_pEnd )
{
pFlags->pHead = this->m_pStart;
}
if ( pFlags->pHead == pFlags->pTail )
{
pFlags->blOverrun = TRUE;
}
}
}
The problem described above occurs when the pFlags->pHead += sizeof(T);of IncrementHead.