structures memory alignment

This topic is 4800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, if i have two structures such as these:
struct StructA {
..members
..of varying
..types etc.
};
struct StructB {
StructA	a;

..more
..members
};
StructB		myStruct;


does cpp guarantee that &myStruct == &myStruct.a ? if not, is there any other way i can solve the following problem? a function takes a pointer to a StructA, and it uses its members for varying purposes when it completes it calls a different function, specified by me, and passes along the StructA pointer i want to be able to send more information to my second function, but the first function needs the elements of StructA to be in the memory it expects them to be from just passing a StructA this is what i want to do:
struct StructA {
MysteriousType	important;
MysteriousType	changeCausesCrash;
};
struct StructB {
StructA		a;

int		number;
};

void myFunction1() {
StructB		b;

//init b..

systemFunction(&b.a, myFunction2);
}

// The systemFunction will call this function
void myFunction2(StructA *a) {
StructB		*b = (StructB*)a;

..do something with b->number..
}


i want to do this for overlapped i/o in windows xp =)

Share on other sites
Quote:
 Original post by Erik RufeltHi,if i have two structures such as these:*** Source Snippet Removed ***does cpp guarantee that &myStruct == &myStruct.a ?

While it is compiler specific, usually yes.

Share on other sites
Microsoft uses this trick extensively in it's APIs. As an extra sanity check, they require you to set the size of the structure so they can be doubly sure that you've passed the right pointer in. e.g

struct StructA {        size_t          size;	MysteriousType	important;	MysteriousType	changeCausesCrash;};struct StructB {	StructA		a;		int		number;};void myFunction1() {	StructB		b;		//init b..        b.size = sizeof(StructB);		systemFunction(&b.a, myFunction2);}// The systemFunction will call this functionvoid myFunction2(StructA *a) {           if(a->size == sizeof(StructB))        {       	    StructB		*b = (StructB*)a;            //do something with b->number..        }        else        {            //someone's been a very naughty boy and passed in a dodgy pointer.            //handle this gracefully instead of exploding horribly.        }}

Share on other sites
Quote:
 As an extra sanity check, they require you to set the size of the structure so they can be doubly sure that you've passed the right pointer in.

And it's the most horribly idiotic system ever devised. Do not do this in C++.

Share on other sites
Quote:
 Original post by DeyjaAnd it's the most horribly idiotic system ever devised. Do not do this in C++.

True, although it's marginally better than just casting to a different structure and hoping it doesn't crash when you dereference it.

Microsoft interfaces tend to use these hacks for C backwards compatibility reasons. If you're using only C++, then you have more powerful tools at your disposal, like polymorphism and RTTI.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• Forum Statistics

• Total Topics
631785
• Total Posts
3002347
×