468,738 Members | 2,397 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

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

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
2 1734
sicarie
4,677 Expert Mod 4TB
If you want to look at the assembly, you could use GDB - the GNU Debugger.
Jul 2 '07 #2
weaknessforcats
9,207 Expert Mod 8TB
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.

Similar topics

13 posts views Thread by Amarendra | last post: by
10 posts views Thread by ranjeet.gupta | last post: by
28 posts views Thread by kyle york | last post: by
4 posts views Thread by junky_fellow | last post: by
24 posts views Thread by karthikbalaguru | last post: by
10 posts views Thread by Rohit kumar Chandel | last post: by
12 posts views Thread by Kislay | last post: by
1 post views Thread by CARIGAR | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.