Byte alignment
-
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
-
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
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++ -
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++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.
-
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.
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++ -
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
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 -
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.
Thanks! Stan the man