What's the difference between sizeof and alignof?

后端 未结 7 1422
遥遥无期
遥遥无期 2020-12-23 17:19

What\'s the difference between sizeof and alignof?

#include 

#define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << \'/\' << a         


        
相关标签:
7条回答
  • 2020-12-23 18:04

    The two operators do fundamentally different things. sizeof gives the size of a type (how much memory it takes) whereas alignof gives what how many bytes a type must be aligned to. It just so happens that the primitives you tested have an alignment requirement the same as their size (which makes sense if you think about it).

    Think about what happens if you have a struct instead:

    struct Foo {
         int a;
         float b;
         char c;
    };
    

    alignof(Foo) will return 4.

    0 讨论(0)
提交回复
热议问题