Discussion:
Padding question
(too old to reply)
T
2022-11-25 16:11:56 UTC
Permalink
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
Paul N
2022-11-26 13:07:49 UTC
Permalink
Post by T
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.
1) does this mean that between each field, there
will be some amount of throwaway (padding) bytes?
It depends. Suppose int is four bytes and has to be stored at an address which is a multiple of four. If your struct is a char followed by an int, there will be three padding bytes after the char so that the int is properly aligned. If the struct is four chars followed by an int, no padding is needed.
Post by T
2) the beginning and end of the structure will
have no throwaway (padding) bytes?
There should be no padding at the beginning. There may be at the end, so that another struct immediately following is properly aligned.
Post by T
3) will the throwaway bytes have any particular
value?
Not necessarily - probably not.
Post by T
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?
It depends what you mean by "read the structure in". If you simply read a chunk of memory from a disk file, for instance, all of the structure will be overwritten including the padding. If you set the members indivually then they probably won't be. I don't think there are any guarantees though.
Post by T
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?
pWinStationName is a pointer - it is a value telling you where something is stored. So there is no null in the structure, but the memory pointed at will probably contain the name followed by a null (a zero) to show the end of the name.
Loading...