Padding question

4 views
Skip to first unread message

T

unread,
Nov 25, 2022, 11:11:58 AM11/25/22
to
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

unread,
Nov 26, 2022, 8:07:51 AM11/26/22
to
On Friday, November 25, 2022 at 4:11:58 PM UTC, T wrote:
> 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?

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.

> 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.

> 3) will the throwaway bytes have any particular
> value?

Not necessarily - probably not.

> 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.

> 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?

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.
Reply all
Reply to author
Forward
0 new messages