| View previous topic :: View next topic |
| Author |
Message |
Dark Byte Site Admin
Reputation: 474
Joined: 09 May 2003 Posts: 25962 Location: The netherlands
|
Posted: Tue Aug 11, 2009 12:47 pm Post subject: structure length/internal alignment |
|
|
Can someone tell me why the following structure returns size 11 ?
I can fix it by replacing the bit addressable elements with a byte and do the bitwise operations myself, but just wondering, shouldn't the #pragma pack(1) have fixed it, or is that the cause of the problem here ?
| Code: |
#pragma pack(1)
typedef struct _XXX
{
WORD a;
WORD b;
BYTE c;
unsigned d1: 3;
unsigned d2: 1;
unsigned d3: 1;
unsigned d4: 2;
unsigned d5: 1;
WORD e;
} XXX, *PXXX;
#pragma pack()
...
XXX bla;
if (sizeof(bla) != 8)
{
...
}
return ERROR;
|
_________________
Tools give you results. Knowledge gives you control.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
Polynomial Grandmaster Cheater
Reputation: 5
Joined: 17 Feb 2008 Posts: 524 Location: Inside the Intel CET shadow stack
|
Posted: Tue Aug 11, 2009 1:42 pm Post subject: |
|
|
Have you tried specifying them as unsigned short? I know it's the same thing technically, but I've seen a few posts regarding this issue that seem to be similar to this. Maybe it's a compiler bug.
| Code: | #pragma pack(1)
typedef struct _XXX
{
WORD a;
WORD b;
BYTE c;
unsigned short d1: 3;
unsigned short d2: 1;
unsigned short d3: 1;
unsigned short d4: 2;
unsigned short d5: 1;
WORD e;
} XXX, *PXXX;
#pragma pack()
...
XXX bla;
if (sizeof(bla) != 8)
{
...
}
return ERROR; |
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time. |
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
Posted: Tue Aug 11, 2009 3:08 pm Post subject: |
|
|
| Code: |
#pragma pack(1)
typedef struct _XXX
{
WORD a;
WORD b;
BYTE c;
unsigned char d1: 3;
unsigned char d2: 1;
unsigned char d3: 1;
unsigned char d4: 2;
unsigned char d5: 1;
WORD e;
} XXX, *PXXX;
#pragma pack()
XXX bla = {0};
DWORD blah = sizeof(bla);
|
C++ defaults to int for unspecified types..
this works as I think this is what you where trying to do..
regards BanMe
_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you. |
|
| Back to top |
|
 |
Polynomial Grandmaster Cheater
Reputation: 5
Joined: 17 Feb 2008 Posts: 524 Location: Inside the Intel CET shadow stack
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Wed Aug 12, 2009 1:28 pm Post subject: |
|
|
| Code: | #include <stdio.h>
#include <stddef.h>
#pragma pack(1)
typedef struct _XXX
{
unsigned short a;
unsigned short b;
unsigned char c;
union {
unsigned char d1: 3;
unsigned char d2: 1;
unsigned char d3: 1;
unsigned char d4: 2;
unsigned char d5: 1;
} d;
unsigned short e;
} XXX, *PXXX;
int main()
{
printf("%d\n", sizeof(XXX));
printf("%d ", offsetof(XXX, a));
printf("%d ", offsetof(XXX, b));
printf("%d ", offsetof(XXX, c));
printf("%d ", offsetof(XXX, d));
printf("%d ", offsetof(XXX, e));
return 0;
} |
Since you are using microsoft specific defines, you could also use __declspec(align(1)) if you so desired.
|
|
| Back to top |
|
 |
|