私はアプリケーションを書いていますが、いくつかのポインター演算を行う必要がありました。ただし、このアプリケーションは異なるアーキテクチャで実行されます! これが問題になるかどうかはよくわかりませんでしたが、この記事を読んだ後、変更する必要があると思いました.
あまり好きではなかった元のコードは次のとおりです。
class Frame{
/* ... */
protected:
const u_char* const m_pLayerHeader; // Where header of this layer starts
int m_iHeaderLength; // Length of the header of this layer
int m_iFrameLength; // Header + payloads length
};
/**
* Get the pointer to the payload of the current layer
* @return A pointer to the payload of the current layer
*/
const u_char* Frame::getPayload() const
{
// FIXME : Pointer arithmetic, portability!
return m_pLayerHeader + m_iHeaderLength;
}
かなり悪いですね!ポインタにint
値を追加!u_char
しかし、私はこれに変更しました:
const u_char* Frame::getPayload() const
{
return &m_pLayerHeader[m_iHeaderLength];
}
私は今、コンパイラがジャンプする量を言うことができると思います! 右?配列に対する操作は[]
ポインター演算と見なされますか? 移植性の問題は解決しますか?