Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Byte alignment

Byte alignment

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stan the man
    wrote on last edited by
    #1

    Hi. We are doing TCPIP communications between an old system (DOS) and a WinXP system. We put all the message contents into a struture. Because the DOS side we can not change, we encountered a few problems that seems to be related to byte alignment. I think on the windows side, it needs to be 4 byte aligned. But on the DOS side it seems to be 2 byte aligned. As a result, it seems when you access a specific variable using pointers or use "sizeof" it gives the wrong result. Is there anyway to force the alignment correctly so that nothing will be "padded"? Thanks in adavnce. Stan the man

    C J 2 Replies Last reply
    0
    • S Stan the man

      Hi. We are doing TCPIP communications between an old system (DOS) and a WinXP system. We put all the message contents into a struture. Because the DOS side we can not change, we encountered a few problems that seems to be related to byte alignment. I think on the windows side, it needs to be 4 byte aligned. But on the DOS side it seems to be 2 byte aligned. As a result, it seems when you access a specific variable using pointers or use "sizeof" it gives the wrong result. Is there anyway to force the alignment correctly so that nothing will be "padded"? Thanks in adavnce. Stan the man

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Use pragma pack around your structure definition:

      #pragma pack(2)
      struct MyStruct
      {
      ..
      ..
      };
      // Reset to default
      #pragma pack()

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      S 1 Reply Last reply
      0
      • C Cedric Moonen

        Use pragma pack around your structure definition:

        #pragma pack(2)
        struct MyStruct
        {
        ..
        ..
        };
        // Reset to default
        #pragma pack()

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        S Offline
        S Offline
        Stan the man
        wrote on last edited by
        #3

        HI. Thanks for the reply. IT only needs to be around the definition call. I do not have to do anything else when I call it or use it then, right? Just want to be sure... Thanks.

        C S 2 Replies Last reply
        0
        • S Stan the man

          HI. Thanks for the reply. IT only needs to be around the definition call. I do not have to do anything else when I call it or use it then, right? Just want to be sure... Thanks.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Stan the man wrote:

          IT only needs to be around the definition call

          What do you mean by "definition call" ? Anyway, it only needs to be around the structure definition. When you use the structure afterwards, the byte alignment will be on two bytes (in this case).

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • S Stan the man

            Hi. We are doing TCPIP communications between an old system (DOS) and a WinXP system. We put all the message contents into a struture. Because the DOS side we can not change, we encountered a few problems that seems to be related to byte alignment. I think on the windows side, it needs to be 4 byte aligned. But on the DOS side it seems to be 2 byte aligned. As a result, it seems when you access a specific variable using pointers or use "sizeof" it gives the wrong result. Is there anyway to force the alignment correctly so that nothing will be "padded"? Thanks in adavnce. Stan the man

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            In addition to what Cedric wrote, note that packing alignment may only be part of the problem. Type sizes may be different in old DOS-based software, so an struct member of type int may only be 2-bytes wide in DOS as opposed to 4-bytes wide in Win32.    Peace!

            -=- James
            Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            See DeleteFXPFiles

            1 Reply Last reply
            0
            • S Stan the man

              HI. Thanks for the reply. IT only needs to be around the definition call. I do not have to do anything else when I call it or use it then, right? Just want to be sure... Thanks.

              S Offline
              S Offline
              Stan the man
              wrote on last edited by
              #6

              Thanks! Stan the man

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups