Solved

Memory alignment of intrinsic type vs structure query

Posted on 2014-12-12
10
188 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to creat good software interface ? 1 67
I could not build boost code, 10 87
c++ syntax question 9 48
Constant string is of type char *   ? 7 29
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

776 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