Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Memory alignment of intrinsic type vs structure query

Posted on 2014-12-12
10
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

670 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