Why “Alignment” return same result between 32bit and 64bit system?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







7















I am wondering will compiler make padding result different between 32bit and 64bit system. So I write the code below in a simple VS2019 C++ console project today.



struct Z
{
char s;
__int64 i;
};

int main()
{
std::cout << sizeof(Z) <<"n";
}


What I expected on each "Platform" setting



x86: 12
X64: 16


Actual result



x86: 16
X64: 16


Since the memory word size on x86 is 4 byte. This means it has to store the 16 bytes 'i' in two different words. So I thought the compiler will do padding in this way:



struct Z
{
char s;
char _pad[3];
__int64 i;
};


So may I know what's the reason behind this?




  1. For forward-compatibility with the 64bit system?

  2. Due to the limitation of supporting 64bit number on the 32bit processor?










share|improve this question









New contributor




Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    7















    I am wondering will compiler make padding result different between 32bit and 64bit system. So I write the code below in a simple VS2019 C++ console project today.



    struct Z
    {
    char s;
    __int64 i;
    };

    int main()
    {
    std::cout << sizeof(Z) <<"n";
    }


    What I expected on each "Platform" setting



    x86: 12
    X64: 16


    Actual result



    x86: 16
    X64: 16


    Since the memory word size on x86 is 4 byte. This means it has to store the 16 bytes 'i' in two different words. So I thought the compiler will do padding in this way:



    struct Z
    {
    char s;
    char _pad[3];
    __int64 i;
    };


    So may I know what's the reason behind this?




    1. For forward-compatibility with the 64bit system?

    2. Due to the limitation of supporting 64bit number on the 32bit processor?










    share|improve this question









    New contributor




    Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      7












      7








      7








      I am wondering will compiler make padding result different between 32bit and 64bit system. So I write the code below in a simple VS2019 C++ console project today.



      struct Z
      {
      char s;
      __int64 i;
      };

      int main()
      {
      std::cout << sizeof(Z) <<"n";
      }


      What I expected on each "Platform" setting



      x86: 12
      X64: 16


      Actual result



      x86: 16
      X64: 16


      Since the memory word size on x86 is 4 byte. This means it has to store the 16 bytes 'i' in two different words. So I thought the compiler will do padding in this way:



      struct Z
      {
      char s;
      char _pad[3];
      __int64 i;
      };


      So may I know what's the reason behind this?




      1. For forward-compatibility with the 64bit system?

      2. Due to the limitation of supporting 64bit number on the 32bit processor?










      share|improve this question









      New contributor




      Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I am wondering will compiler make padding result different between 32bit and 64bit system. So I write the code below in a simple VS2019 C++ console project today.



      struct Z
      {
      char s;
      __int64 i;
      };

      int main()
      {
      std::cout << sizeof(Z) <<"n";
      }


      What I expected on each "Platform" setting



      x86: 12
      X64: 16


      Actual result



      x86: 16
      X64: 16


      Since the memory word size on x86 is 4 byte. This means it has to store the 16 bytes 'i' in two different words. So I thought the compiler will do padding in this way:



      struct Z
      {
      char s;
      char _pad[3];
      __int64 i;
      };


      So may I know what's the reason behind this?




      1. For forward-compatibility with the 64bit system?

      2. Due to the limitation of supporting 64bit number on the 32bit processor?







      c++ visual-c++






      share|improve this question









      New contributor




      Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 50 mins ago









      Quentin

      47.1k692148




      47.1k692148






      New contributor




      Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 54 mins ago









      Shen YuanShen Yuan

      361




      361




      New contributor




      Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Shen Yuan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          3 Answers
          3






          active

          oldest

          votes


















          5














          The padding is not determined by the word size, but by the alignment of each data type.



          In most cases, the alignment requirement is equal to the type's size. So for a 64 bit type like int64 you will get an 8 byte (64 bit) alignment. Padding needs to be inserted into the struct to make sure that the storage for the type ends up at an address that is properly aligned.



          You may see a difference in padding between 32 bit and 64 bit when using built-in datatypes that have different sizes on both architectures, for instance pointer types (int*).






          share|improve this answer


























          • Default alignment is determined by wordsize however. Reason beeing that words are adressed in memory so that they fit into registers perfectly. On x86(_64) unaligned data requires a shift operation to work with it. On other paltforms like sun sparc unaligned data will cause a bus exception. If you want to remove padding try adding __attribute__((packed)) (GCC) to the struct definition.

            – Nefrin
            12 mins ago











          • @Nefrin Do you have a reference for that? I am not aware of any such behavior for the C or C++ built-in datatypes.

            – ComicSansMS
            9 mins ago



















          0














          "32-bit" and "64-bit" refer to the size/alignment of pointers. If you use void* instead of __int64, you will see your expected result.



          Any x86-based system could store its __int64 field starting from an address not divisible by 8, but that would be inefficient - for example, this could split it between two cache lines. So all "basic" data types require their alignment to be a multiple of their size.






          share|improve this answer































            0














            This is a matter of alignment requirement of the data type as specified in
            Padding and Alignment of Structure Members




            Every data object has an alignment-requirement. The alignment-requirement for all data except structures, unions, and arrays is either the size of the object or the current packing size (specified with either /Zp or the pack pragma, whichever is less).




            And the default value for structure member alignment is specified in /Zp (Struct Member Alignment)




            The available packing values are described in the following table:



            /Zp argument Effect

            1 Packs structures on 1-byte boundaries. Same as /Zp.

            2 Packs structures on 2-byte boundaries.

            4 Packs structures on 4-byte boundaries.
            8 Packs structures on 8-byte boundaries (default for x86, ARM, and ARM64).

            16 Packs structures on 16-byte boundaries (default for x64).




            Since the default for x86 is /Zp8 which is 8 bytes, the output is 16.



            However, you can specify a different packing size with /Zp option.

            Here is a Live Demo with /Zp4 which gives the output as 12 instead of 16.






            share|improve this answer


























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });






              Shen Yuan is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55920103%2fwhy-alignment-return-same-result-between-32bit-and-64bit-system%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              The padding is not determined by the word size, but by the alignment of each data type.



              In most cases, the alignment requirement is equal to the type's size. So for a 64 bit type like int64 you will get an 8 byte (64 bit) alignment. Padding needs to be inserted into the struct to make sure that the storage for the type ends up at an address that is properly aligned.



              You may see a difference in padding between 32 bit and 64 bit when using built-in datatypes that have different sizes on both architectures, for instance pointer types (int*).






              share|improve this answer


























              • Default alignment is determined by wordsize however. Reason beeing that words are adressed in memory so that they fit into registers perfectly. On x86(_64) unaligned data requires a shift operation to work with it. On other paltforms like sun sparc unaligned data will cause a bus exception. If you want to remove padding try adding __attribute__((packed)) (GCC) to the struct definition.

                – Nefrin
                12 mins ago











              • @Nefrin Do you have a reference for that? I am not aware of any such behavior for the C or C++ built-in datatypes.

                – ComicSansMS
                9 mins ago
















              5














              The padding is not determined by the word size, but by the alignment of each data type.



              In most cases, the alignment requirement is equal to the type's size. So for a 64 bit type like int64 you will get an 8 byte (64 bit) alignment. Padding needs to be inserted into the struct to make sure that the storage for the type ends up at an address that is properly aligned.



              You may see a difference in padding between 32 bit and 64 bit when using built-in datatypes that have different sizes on both architectures, for instance pointer types (int*).






              share|improve this answer


























              • Default alignment is determined by wordsize however. Reason beeing that words are adressed in memory so that they fit into registers perfectly. On x86(_64) unaligned data requires a shift operation to work with it. On other paltforms like sun sparc unaligned data will cause a bus exception. If you want to remove padding try adding __attribute__((packed)) (GCC) to the struct definition.

                – Nefrin
                12 mins ago











              • @Nefrin Do you have a reference for that? I am not aware of any such behavior for the C or C++ built-in datatypes.

                – ComicSansMS
                9 mins ago














              5












              5








              5







              The padding is not determined by the word size, but by the alignment of each data type.



              In most cases, the alignment requirement is equal to the type's size. So for a 64 bit type like int64 you will get an 8 byte (64 bit) alignment. Padding needs to be inserted into the struct to make sure that the storage for the type ends up at an address that is properly aligned.



              You may see a difference in padding between 32 bit and 64 bit when using built-in datatypes that have different sizes on both architectures, for instance pointer types (int*).






              share|improve this answer















              The padding is not determined by the word size, but by the alignment of each data type.



              In most cases, the alignment requirement is equal to the type's size. So for a 64 bit type like int64 you will get an 8 byte (64 bit) alignment. Padding needs to be inserted into the struct to make sure that the storage for the type ends up at an address that is properly aligned.



              You may see a difference in padding between 32 bit and 64 bit when using built-in datatypes that have different sizes on both architectures, for instance pointer types (int*).







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 43 mins ago

























              answered 48 mins ago









              ComicSansMSComicSansMS

              33.7k691118




              33.7k691118













              • Default alignment is determined by wordsize however. Reason beeing that words are adressed in memory so that they fit into registers perfectly. On x86(_64) unaligned data requires a shift operation to work with it. On other paltforms like sun sparc unaligned data will cause a bus exception. If you want to remove padding try adding __attribute__((packed)) (GCC) to the struct definition.

                – Nefrin
                12 mins ago











              • @Nefrin Do you have a reference for that? I am not aware of any such behavior for the C or C++ built-in datatypes.

                – ComicSansMS
                9 mins ago



















              • Default alignment is determined by wordsize however. Reason beeing that words are adressed in memory so that they fit into registers perfectly. On x86(_64) unaligned data requires a shift operation to work with it. On other paltforms like sun sparc unaligned data will cause a bus exception. If you want to remove padding try adding __attribute__((packed)) (GCC) to the struct definition.

                – Nefrin
                12 mins ago











              • @Nefrin Do you have a reference for that? I am not aware of any such behavior for the C or C++ built-in datatypes.

                – ComicSansMS
                9 mins ago

















              Default alignment is determined by wordsize however. Reason beeing that words are adressed in memory so that they fit into registers perfectly. On x86(_64) unaligned data requires a shift operation to work with it. On other paltforms like sun sparc unaligned data will cause a bus exception. If you want to remove padding try adding __attribute__((packed)) (GCC) to the struct definition.

              – Nefrin
              12 mins ago





              Default alignment is determined by wordsize however. Reason beeing that words are adressed in memory so that they fit into registers perfectly. On x86(_64) unaligned data requires a shift operation to work with it. On other paltforms like sun sparc unaligned data will cause a bus exception. If you want to remove padding try adding __attribute__((packed)) (GCC) to the struct definition.

              – Nefrin
              12 mins ago













              @Nefrin Do you have a reference for that? I am not aware of any such behavior for the C or C++ built-in datatypes.

              – ComicSansMS
              9 mins ago





              @Nefrin Do you have a reference for that? I am not aware of any such behavior for the C or C++ built-in datatypes.

              – ComicSansMS
              9 mins ago













              0














              "32-bit" and "64-bit" refer to the size/alignment of pointers. If you use void* instead of __int64, you will see your expected result.



              Any x86-based system could store its __int64 field starting from an address not divisible by 8, but that would be inefficient - for example, this could split it between two cache lines. So all "basic" data types require their alignment to be a multiple of their size.






              share|improve this answer




























                0














                "32-bit" and "64-bit" refer to the size/alignment of pointers. If you use void* instead of __int64, you will see your expected result.



                Any x86-based system could store its __int64 field starting from an address not divisible by 8, but that would be inefficient - for example, this could split it between two cache lines. So all "basic" data types require their alignment to be a multiple of their size.






                share|improve this answer


























                  0












                  0








                  0







                  "32-bit" and "64-bit" refer to the size/alignment of pointers. If you use void* instead of __int64, you will see your expected result.



                  Any x86-based system could store its __int64 field starting from an address not divisible by 8, but that would be inefficient - for example, this could split it between two cache lines. So all "basic" data types require their alignment to be a multiple of their size.






                  share|improve this answer













                  "32-bit" and "64-bit" refer to the size/alignment of pointers. If you use void* instead of __int64, you will see your expected result.



                  Any x86-based system could store its __int64 field starting from an address not divisible by 8, but that would be inefficient - for example, this could split it between two cache lines. So all "basic" data types require their alignment to be a multiple of their size.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 48 mins ago









                  anatolyganatolyg

                  17.3k44794




                  17.3k44794























                      0














                      This is a matter of alignment requirement of the data type as specified in
                      Padding and Alignment of Structure Members




                      Every data object has an alignment-requirement. The alignment-requirement for all data except structures, unions, and arrays is either the size of the object or the current packing size (specified with either /Zp or the pack pragma, whichever is less).




                      And the default value for structure member alignment is specified in /Zp (Struct Member Alignment)




                      The available packing values are described in the following table:



                      /Zp argument Effect

                      1 Packs structures on 1-byte boundaries. Same as /Zp.

                      2 Packs structures on 2-byte boundaries.

                      4 Packs structures on 4-byte boundaries.
                      8 Packs structures on 8-byte boundaries (default for x86, ARM, and ARM64).

                      16 Packs structures on 16-byte boundaries (default for x64).




                      Since the default for x86 is /Zp8 which is 8 bytes, the output is 16.



                      However, you can specify a different packing size with /Zp option.

                      Here is a Live Demo with /Zp4 which gives the output as 12 instead of 16.






                      share|improve this answer






























                        0














                        This is a matter of alignment requirement of the data type as specified in
                        Padding and Alignment of Structure Members




                        Every data object has an alignment-requirement. The alignment-requirement for all data except structures, unions, and arrays is either the size of the object or the current packing size (specified with either /Zp or the pack pragma, whichever is less).




                        And the default value for structure member alignment is specified in /Zp (Struct Member Alignment)




                        The available packing values are described in the following table:



                        /Zp argument Effect

                        1 Packs structures on 1-byte boundaries. Same as /Zp.

                        2 Packs structures on 2-byte boundaries.

                        4 Packs structures on 4-byte boundaries.
                        8 Packs structures on 8-byte boundaries (default for x86, ARM, and ARM64).

                        16 Packs structures on 16-byte boundaries (default for x64).




                        Since the default for x86 is /Zp8 which is 8 bytes, the output is 16.



                        However, you can specify a different packing size with /Zp option.

                        Here is a Live Demo with /Zp4 which gives the output as 12 instead of 16.






                        share|improve this answer




























                          0












                          0








                          0







                          This is a matter of alignment requirement of the data type as specified in
                          Padding and Alignment of Structure Members




                          Every data object has an alignment-requirement. The alignment-requirement for all data except structures, unions, and arrays is either the size of the object or the current packing size (specified with either /Zp or the pack pragma, whichever is less).




                          And the default value for structure member alignment is specified in /Zp (Struct Member Alignment)




                          The available packing values are described in the following table:



                          /Zp argument Effect

                          1 Packs structures on 1-byte boundaries. Same as /Zp.

                          2 Packs structures on 2-byte boundaries.

                          4 Packs structures on 4-byte boundaries.
                          8 Packs structures on 8-byte boundaries (default for x86, ARM, and ARM64).

                          16 Packs structures on 16-byte boundaries (default for x64).




                          Since the default for x86 is /Zp8 which is 8 bytes, the output is 16.



                          However, you can specify a different packing size with /Zp option.

                          Here is a Live Demo with /Zp4 which gives the output as 12 instead of 16.






                          share|improve this answer















                          This is a matter of alignment requirement of the data type as specified in
                          Padding and Alignment of Structure Members




                          Every data object has an alignment-requirement. The alignment-requirement for all data except structures, unions, and arrays is either the size of the object or the current packing size (specified with either /Zp or the pack pragma, whichever is less).




                          And the default value for structure member alignment is specified in /Zp (Struct Member Alignment)




                          The available packing values are described in the following table:



                          /Zp argument Effect

                          1 Packs structures on 1-byte boundaries. Same as /Zp.

                          2 Packs structures on 2-byte boundaries.

                          4 Packs structures on 4-byte boundaries.
                          8 Packs structures on 8-byte boundaries (default for x86, ARM, and ARM64).

                          16 Packs structures on 16-byte boundaries (default for x64).




                          Since the default for x86 is /Zp8 which is 8 bytes, the output is 16.



                          However, you can specify a different packing size with /Zp option.

                          Here is a Live Demo with /Zp4 which gives the output as 12 instead of 16.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 3 mins ago

























                          answered 35 mins ago









                          P.WP.W

                          19.6k41961




                          19.6k41961






















                              Shen Yuan is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              Shen Yuan is a new contributor. Be nice, and check out our Code of Conduct.













                              Shen Yuan is a new contributor. Be nice, and check out our Code of Conduct.












                              Shen Yuan is a new contributor. Be nice, and check out our Code of Conduct.
















                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55920103%2fwhy-alignment-return-same-result-between-32bit-and-64bit-system%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              GameSpot

                              connect to host localhost port 22: Connection refused

                              Getting a Wifi WPA2 wifi connection