Memory alignment of intrinsic type vs structure query

Ah hello.  

I am looking at padding and alignment, running on 32-bit Windows 7 using VS 2008.  I understand how alignment and required padding work generally, but the following code has raised queries:

struct TestStruct
{
	char *pC;
	char cC;
	int xX;
};

int main(int argc, char **argv)
{
	char *pC = (char*)0xDEADBEEF;
	char cC = 'A';
	int xX = 0xAF;

	TestStruct test = { (char*)0xDEADBEEF, 'A', 0xAF };

	return 0;
}

Open in new window

Now, when I look at the memory window in, pointing it at &xX (my local stack variable that appears at the lowest address (excluding the structure)), it shows me the following:

0x0024FB5C  af 00 00 00 cc cc cc cc cc cc cc cc cc cc cc 41 cc cc cc cc  
0x0024FB70  cc cc cc cc ef be ad de cc cc cc cc 20 cf 25 06 cc fb 24 00

Open in new window


Clearly we can see the integer as the first 4bytes in little endian form, 11 bytes of padding, the single character, 8 bytes of padding then my char in little endian form*.

When I look at the structure (&test) in the memory window however, I see

0x0027FC4C  ef be ad de 41 cc cc cc af 00 00 00

Open in new window


Much less padding!

My question is simple: why is there some much additional padding when the variables are declared individually?
LVL 19
mrwad99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Try to compile that as "Release" and the padding will be gone. The compiler adds 'guards' to be able to detect stack corruption. BTW, check out http://msdn.microsoft.com/en-us/library/vstudio/fsk896zz%28v=vs.90%29.aspx ("How to: Debug a Release Build") on how to test that.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrwad99Author Commented:
Thanks for commenting.

Hah, yes: I should have figured it was a debug mode thing.

However, I read the link and found that all those options are currently set; however when I try and view the memory at &xX the memory window says "Unable to evaluate the expression. ".

So it seems that I cannot prove what you are saying...

Any suggestions?
0
phoffricCommented:
In release mode, your variables have nothing to live for. If you look at the disassembly, you should see them optimized out. Play around with giving your variables a reason to exist, and verify in the disassembly. You may have to have two .cpp files and/or assign values from input to avoid optimization.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jkrCommented:
Um, if you followed theinstructions on the MSDN page (i.e. also turned of optimizations) there should be absolutely no differene - given that the .pdb file resides in the same directory. So I am a little stumoed...
0
mrwad99Author Commented:
OK; thanks both.  That explains that; I added some sscanf calls

scanf("%c", &cC);
	scanf("%i", &xX);
	scanf("%p", &pC);

Open in new window

and I can now see the values in memory.  However, this has confused me further:

I entered a space, 11 then BEEFDEAD for my scanf calls, and the memory layout is as follows:

0x001AF9BF  20 ad de ef be 0b 00 00 00

Open in new window


We have the space, the pointer then the integer.  THe pointer starts on an address divisible fby 4, so that is ok, but what I don't get is why the char appears first: I have read that stack variables appear in memory in the order they are defined, starting at the top of the stack then working down, and I declared my variables as

	char *pC = (char*)0xDEADBEEF;
	char cC = 'A';
	int xX = 0x000000AF;

Open in new window


so surely xX should appear first, then the character, then the pointer, since the VS memory window shows memory addresses in increasing order...

In debug I get this (with the extra padding): integer, character, pointer:
0b 00 00 00 cc cc cc cc cc cc cc cc cc cc cc 20 cc cc cc cc cc cc cc cc ad de ef be

Open in new window


Any suggestions?
0
phoffricCommented:
>>  I have read that stack variables appear in memory in the order they are defined, starting at the top of the stack then working down
   What is the link for this statement?

You can add:
printf("addresses: pC(%p) cC(%p) xX(%p)", &pC, &cC, &xX);

Open in new window

and run in Debug and Release mode on VS and see different ordering of local variables in the stackframe (as you have already confirmed using debug memory window). (Using g++, the ordering was the same for both Debug (-g) and Release (-O2) modes.)

I looked in the C-spec and did not find the word "stack". In C++ spec, only usage was for the container stack or in reference to stack unwinding. This suggests that the ordering of local variables in the stackframe is compiler implementation dependent.
0
mrwad99Author Commented:
I looked back at where I first heard this, and it was in a Linux programming book, so I guess it doesn't really apply here.  I have read on numerous other sites however that it depends on the processor, so why the debug and release versions differ is still a mystery.

Either way, I have answers to my original question.

Thanks both, and happy holidays :)
0
phoffricCommented:
happy holidays to you and jkr! :)
0
jkrCommented:
Holidays? Wait a second, I've still way over a week of C# and Objective-C to get over with! :-D
(not to speak of the crazy customers)
0
phoffricCommented:
Yeah, me too. lol
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.