Solved

Memory alignment of intrinsic type vs structure query

Posted on 2014-12-12
10
184 Views
Last Modified: 2014-12-15
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?
0
Comment
Question by:mrwad99
  • 4
  • 3
  • 3
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 40497069
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
 
LVL 19

Author Comment

by:mrwad99
ID: 40498029
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
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 40498352
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
 
LVL 86

Expert Comment

by:jkr
ID: 40498361
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
 
LVL 19

Author Comment

by:mrwad99
ID: 40498900
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

Expert Comment

by:phoffric
ID: 40499140
>>  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
 
LVL 19

Author Comment

by:mrwad99
ID: 40501100
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
 
LVL 32

Expert Comment

by:phoffric
ID: 40501697
happy holidays to you and jkr! :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 40501861
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
 
LVL 32

Expert Comment

by:phoffric
ID: 40501994
Yeah, me too. lol
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now