Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:42:34+00:00 2026-05-22T01:42:34+00:00

I have written a template class for a circular buffer: template <class T> class

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:42:34+00:00Added an answer on May 22, 2026 at 1:42 am

    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;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code: #include <iostream> using namespace std; template <class T>
I have written some code in my VB.NET application to send an HTML e-mail
I've written a C++ matrix template class. It's parameterized by its dimensions and by
I have a class that represents some file data. The file contains some headers.
I have written an AIR Application that downloads videos and documents from a server.
I have written a site in Prototype but want to switch to jQuery. Any
I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
I have written an assembly I don't want other people to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.