Question

I have written a template class for a circular buffer:

template <class T> class CRingBuffer { /* ... */ };

Some of the operations this class performs rely on an accurate evaluation of the size of T. This seems to work okay when T is BYTE (i.e. sizeof(T) == 1, check). However, when I try to use the same class where T is DWORD, for some reason sizeof(T) evaluates to 16. The last time I checked, a double-word is 4 bytes, not 16. Does anyone know why this is happening? Thanks.

ADDITIONAL INFO

I can't post all the code due to its proprietary nature, but here is the class declaration and the function definition in question:

template <class T> class CRingBuffer
{
#pragma pack( push , 1 )                // align on a 1-byte boundary

typedef struct BUFFER_FLAGS_tag
{
    T * pHead;                          // Points to next buffer location to write
    T * pTail;                          // Points to next buffer location to read
    BOOL blFull;                        // Indicates whether buffer is full.
    BOOL blEmpty;                       // Indicates whether buffer is empty.
    BOOL blOverrun;                     // Indicates buffer overrun.
    BOOL blUnderrun;                    // Indicates buffer underrun.
    DWORD dwItemCount;                  // Buffer item count.
} BUFFER_FLAGS, *LPBUFFER_FLAGS;

#pragma pack( pop )                     // end 1-byte boundary alignment

    // Private member variable declarations
private:
    T * m_pBuffer;                      // Buffer location in system memory
    T * m_pStart;                       // Buffer start location in system memory
    T * m_pEnd;                         // Buffer end location in system memory
    BUFFER_FLAGS m_tFlags;              // Buffer flags.
    DWORD m_dwCapacity;                 // The buffer capacity.

    // CRingBuffer
public:
    CRingBuffer( DWORD items = DEFAULT_BUF_SIZE );
    ~CRingBuffer();

    // Public member function declarations
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 member function declarations
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;

    // Verify overrun condition is not set.
    if ( pFlags->blOverrun == FALSE )
    {
        pFlags->pHead += sizeof(T); // increament buffer head pointer
        pFlags->blUnderrun = FALSE; // clear underrun condition

        // Correct for wrap condition.
        if ( pFlags->pHead == this->m_pEnd )
        {
            pFlags->pHead = this->m_pStart;
        }

        // Check for overrun.
        if ( pFlags->pHead == pFlags->pTail )
        {
            pFlags->blOverrun = TRUE;
        }
    }
}

The problem described above occurs when pFlags->pHead += sizeof(T); of IncrementHead is executed.

Was it helpful?

Solution

Oh, this is really simple after all :)

Without realising it, in pFlags->pHead += sizeof(T); you use pointer arithmetic. pHead is a pointer to T, and when you increase it by sizeof(T), it means you move it forward by that many elements of type T, and not by that many bytes as you thought. So the size of T gets squared. If your goal is to move the pointer to the next element of the buffer, you should just increment it by 1: pFlags->pHead += 1;

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