471,850 Members | 1,076 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

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;
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;
struct test_t test;/*= { 13,30, 40};*/

here the output is:
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 1829
4,677 Expert Mod 4TB
If you want to look at the assembly, you could use GDB - the GNU Debugger.
Jul 2 '07 #2
9,208 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. };
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
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.