T
2022-11-25 16:11:56 UTC
Hi All,
Padding and Alignment of Structure Members
https://learn.microsoft.com/en-us/cpp/c-language/padding-and-alignment-of-structure-members?view=msvc-170
For structures, unions, and arrays, the
alignment-requirement is the largest alignment-
requirement of its members. Every object is
allocated an offset so that
offset % alignment-requirement == 0
I will be looking at a structure of three fields.
Questions:
1) does this mean that between each field, there
will be some amount of throwaway (padding) bytes?
2) the beginning and end of the structure will
have no throwaway (padding) bytes?
3) will the throwaway bytes have any particular
value?
4) if I pre-salt the structure with a particular
value (for instance, 0xFF in all the bytes), will
the pre-salts be overwritten in the padding
when I read the structure in?
A padding example (the structure will be looking at):
C++
typedef struct _WTS_SESSION_INFOA {
DWORD SessionId;
LPSTR pWinStationName;
WTS_CONNECTSTATE_CLASS State;
} WTS_SESSION_INFOA, *PWTS_SESSION_INFOA;
int nSize1 = sizeof WTS_SESSION_INFOA; // 24
int nOffset1 = offsetof(WTS_SESSION_INFOA, SessionId); // 0
int nOffset2 = offsetof(WTS_SESSION_INFOA, pWinStationName); // 8
int nOffset3 = offsetof(WTS_SESSION_INFOA, State); // 16
SessionsID is a DWORD, so 4 bytes long. State looks
like 8 bytes long.
Will pWinStationName be terminated with a null?
Many thanks,
-T
Padding and Alignment of Structure Members
https://learn.microsoft.com/en-us/cpp/c-language/padding-and-alignment-of-structure-members?view=msvc-170
For structures, unions, and arrays, the
alignment-requirement is the largest alignment-
requirement of its members. Every object is
allocated an offset so that
offset % alignment-requirement == 0
I will be looking at a structure of three fields.
Questions:
1) does this mean that between each field, there
will be some amount of throwaway (padding) bytes?
2) the beginning and end of the structure will
have no throwaway (padding) bytes?
3) will the throwaway bytes have any particular
value?
4) if I pre-salt the structure with a particular
value (for instance, 0xFF in all the bytes), will
the pre-salts be overwritten in the padding
when I read the structure in?
A padding example (the structure will be looking at):
C++
typedef struct _WTS_SESSION_INFOA {
DWORD SessionId;
LPSTR pWinStationName;
WTS_CONNECTSTATE_CLASS State;
} WTS_SESSION_INFOA, *PWTS_SESSION_INFOA;
int nSize1 = sizeof WTS_SESSION_INFOA; // 24
int nOffset1 = offsetof(WTS_SESSION_INFOA, SessionId); // 0
int nOffset2 = offsetof(WTS_SESSION_INFOA, pWinStationName); // 8
int nOffset3 = offsetof(WTS_SESSION_INFOA, State); // 16
SessionsID is a DWORD, so 4 bytes long. State looks
like 8 bytes long.
Will pWinStationName be terminated with a null?
Many thanks,
-T