Jump to content
  • Advertisement
Sign in to follow this  
Erik Rufelt

structures memory alignment

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Erik Rufelt
Hi,

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 this post


Link to post
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 function
void 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Deyja
And 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!