Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


structure length/internal alignment

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Dark Byte
Site Admin
Reputation: 474

Joined: 09 May 2003
Posts: 25962
Location: The netherlands

PostPosted: Tue Aug 11, 2009 12:47 pm    Post subject: structure length/internal alignment Reply with quote

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
View user's profile Send private message MSN Messenger
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Tue Aug 11, 2009 1:42 pm    Post subject: Reply with quote

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
View user's profile Send private message
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Tue Aug 11, 2009 3:08 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Tue Aug 11, 2009 3:11 pm    Post subject: Reply with quote

My bad, I meant unsigned char Razz
_________________
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
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Aug 12, 2009 1:28 pm    Post subject: Reply with quote

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;
}


Code:
8
0 2 4 5 6


Since you are using microsoft specific defines, you could also use __declspec(align(1)) if you so desired.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites