0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

c++ template sizeof() windows linux 注意

Posted at

c++ templeteでsizeof()を使っている時の注意事項

windowsで開発されていたコードをlinux化するというのは開発現場ではよくあることなのだが、仕事中にハマった事について

windowsからlinux化したプログラムの場合、データ型32bit/64bit環境のデータ型の範囲の違いによってバグが出てしまうことがある。
※著者はaws linux2化の開発でハマった。

整数型.jpg

上記表の通り(下記参考URLより抜粋)、
windowsの場合、long型の変数は4byte(32bit/64bit)として扱われるが、
aws linux2の場合は、8byte(64bit)として扱われてしまうことになる。

その場合、以下のようにtemplete,sizeof()を使ってコードを組んでしまうと、開発環境のデータ型範囲の差異によってバグになってしまうことがある。

void templete <class T> test(T _in)
{
    for( int i = 0; i < sizeof(_in) ; i++)
    {
        // 1btyeずつ処理するxxx()という関数を呼び出す場合
        xxx();
    }
}

windowsの場合 :sizeof(_in)=4byteデータとして処理される
aws linux2の場合:sizeof(_in)=8byteデータとして処理される

よって、windowsの場合は4byteデータとして処理されるように実装している為問題ないが、
aws linux2の場合は8byteデータとして処理されるようになってしまうことによって、
xxx()関数内でバグを出してしまう事がある。

よって、windowsからlinux化する時は、sizeof()を使って処理している箇所に問題がない事を検証する事。

(参考URL)
https://marycore.jp/prog/c-lang/data-type-ranges-and-bit-byte-sizes/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?