c++ - 模板类类型调整

标签 c++ visual-studio-2008 templates sizeof circular-buffer

我已经为循环缓冲区编写了一个模板类:

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

此类执行的某些操作依赖于对 T 大小的准确评估。这似乎在 TBYTE 时工作正常(即 sizeof(T) == 1,检查)。但是,当我尝试使用 TDWORD 的同一类时,由于某种原因 sizeof(T) 的计算结果为 16。最后一次我检查过,一个双字是 4 个字节,而不是 16 个字节。有人知道为什么会这样吗?谢谢。

附加信息

由于其专有性质,我无法发布所有代码,但这里是相关的类声明和函数定义:

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;
        }
    }
}

上述问题发生在执行IncrementHeadpFlags->pHead += sizeof(T);时。

最佳答案

哦,毕竟这真的很简单:)

在没有意识到的情况下,在 pFlags->pHead += sizeof(T); 中您使用了指针算法。 pHead 是指向 T 的指针,当您将它增加 sizeof(T) 时,意味着您将它向前移动了那么多元素键入 T,而不是像您想象的那么多字节。所以 T 的大小是平方的。如果您的目标是将指针移动到缓冲区的下一个元素,您应该将其递增 1:pFlags->pHead += 1;

关于c++ - 模板类类型调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5984807/

相关文章:

c++ - 在 Visual C++ 2008 中生成访问器

c++ - 在 Windows 7 上编译 C++ 应用程序,但在 Win2003 Server 上执行它

c++ - 具有三元的简单 C++11 constexpr 阶乘超出了最大模板深度

c++ - 在 C++ 中创建动态类型

c++ - 什么是 "Expression SFINAE"?

c++ - 如何使用 Qt 处理 json 中编码的西里尔字母数据?

c++ - std::set 和 < 运算符重载的奇怪行为?

c++ - 是否应该从命名空间库中向前声明类?

c# - 多个 .NET 配置文件和安装项目问题

c++ - 哪个 "standard"是 C++11 标准?