sizeofとalignofの違いは何ですか?sizeofとalignofの違いは何ですか?
#include <iostream>
#define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << '/' << alignof(T) << std::endl
int main(int, char**)
{
SIZEOF_ALIGNOF(unsigned char);
SIZEOF_ALIGNOF(char);
SIZEOF_ALIGNOF(unsigned short int);
SIZEOF_ALIGNOF(short int);
SIZEOF_ALIGNOF(unsigned int);
SIZEOF_ALIGNOF(int);
SIZEOF_ALIGNOF(float);
SIZEOF_ALIGNOF(unsigned long int);
SIZEOF_ALIGNOF(long int);
SIZEOF_ALIGNOF(unsigned long long int);
SIZEOF_ALIGNOF(long long int);
SIZEOF_ALIGNOF(double);
}
意志出力
1/1 1/1 2/2 2/2 4/4 4/4 4/4 4/4 4/4 8/8 8/8 8/8
私はアライメントが何であるか分からないと思います...?
これをネイティブタイプの代わりに構造体で再度試してください。 –
'指定された型のインスタンスに必要な整列をバイト数(2の整数乗)で返す ' - http://en.cppreference.com/w/cpp/language/alignof。 'sizeof'はもちろんバイト単位のサイズを与えます。 – chris
多分言及する価値がある - [sizeofは常にalignofの倍数です](http://stackoverflow.com/questions/4637774/is-the-size-of-a-struct-required-to-be-an-exact-multiple -of-the-alignment-of-tha) – Steve314