By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

How can I peek at the assembly code generated by GCC -- Structure padding.

P: 22
gcc -O2 -S -c b.c gives the answer but if we declare a structure say
struct test_t {
int a;
char b;
int c;
};
main()
{
struct test_t test= { 13,30, 40};
}

Now in command prompt if we type gcc -O2 -S -c b.c will give output in b.s file as
30 .align 4
31 .LC0:
32 0000 0D000000 .long 13
33 0004 1E .byte 30
34 0005 000000 .zero 3 ( structure padding of 3 Bytes)
35 0008 28000000 .long 40

with the help of gcc -S command we can see the padding done by compiler but if we don't

declare the structure say:

struct test_t {
int a;
char b;
int c;
};
main()
{
struct test_t test;/*= { 13,30, 40};*/
}

here the output is:
.LC0:
31 0000 256400 .string "%d"
32 .text
33 .align 4
35 .globl main
37 main:..


Here i can't see the structure, only if i assign the structure with a value then only "gcc

-S" is effective. Is their any other way to find padding done by structure.
Jul 2 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
If you want to look at the assembly, you could use GDB - the GNU Debugger.
Jul 2 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
You can calculate the pad yourself.
Expand|Select|Wrap|Line Numbers
  1. struct MyStruct
  2. {
  3.     int a;
  4.     char b;
  5.     int c;
  6. };
  7.  
Here the &a + (sizeof int) should be &b. If not, the difference between &a + (sizeof int) and &b is the pad.

You should find three pad bytes after b.

By &a + (sizeof int) I mean the &a plus (sizeof int) bytes.

You will need to convert your addresses to int to do the calculations.
Jul 2 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.