Split grml-scripts into grml-scripts and grml-scripts-core
[grml-scripts.git] / compile / align.c
1 /*
2  * By Patrick Reynolds <reynolds@cs.duke.edu>
3  * Too trivial to copyright.  It's in the public domain!
4  *
5  * To build:
6  *   gcc align.c -o align
7  *
8  * To run:
9  *   ./align
10  *
11  * Note that optimizing compilers will almost certainly reorder the data
12  * segment, throwing off the values you get for structure alignments.
13  * Annoyingly, some compilers don't let you disable optimizations.
14  * GCC by default (with optimizations off) doesn't reorder data segments.
15  * So use GCC.
16  */
17
18 #include <stdio.h>
19
20 typedef struct { char a; } s_char;
21 typedef struct { short a; } s_short;
22 typedef struct { int a; } s_int;
23 typedef struct { long a; } s_long;
24 typedef struct { long long a; } s_long_long;
25 typedef struct { float a; } s_float;
26 typedef struct { double a; } s_double;
27 typedef struct { int *a; } s_intp;
28
29 int main() {
30   int i = 0x01020304;
31   char *ch = (char*)&i;
32   char c1;        char ch0;  char c2;
33   double d1;      char ch1;  double d2;
34   float f1;       char ch2;  float f2;
35   short s1;       char ch3;  short s2;
36   int i1;         char ch4;  int i2;
37   long long ll1;  char ch5;  long long ll2;
38   int *p1;        char ch6;  int *p2;
39   long l1;        char ch7;  long l2;
40   s_char sc1;     char ch8;  s_char sc2;
41   s_short ss1;    char ch9;  s_short ss2;
42   s_int si1;      char ch10; s_int si2;
43   s_long sl1;     char ch11; s_long sl2;
44   s_long_long sll1;char ch12;s_long_long sll2;
45   s_float sf1;    char ch13; s_float sf2;
46   s_double sd1;   char ch14; s_double sd2;
47   s_intp sp1;     char ch15; s_intp sp2;
48
49   if (ch[0] == 0x01)
50     printf("Big endian (%d %d %d %d)\n",     ch[0], ch[1], ch[2], ch[3]);
51   else if (ch[0] == 0x04)
52     printf("Little endian (%d %d %d %d)\n",  ch[0], ch[1], ch[2], ch[3]);
53   else
54     printf("Unknown endian (%d %d %d %d)\n", ch[0], ch[1], ch[2], ch[3]);
55
56   printf("sizes:\n");
57   printf("  char:      %d\n", sizeof(char));
58   printf("  short:     %d\n", sizeof(short));
59   printf("  int:       %d\n", sizeof(int));
60   printf("  long:      %d\n", sizeof(long));
61   printf("  long long: %d\n", sizeof(long long));
62   printf("  float:     %d\n", sizeof(float));
63   printf("  double:    %d\n", sizeof(double));
64   printf("  int*:      %d\n", sizeof(int*));
65   printf("alignments:\n");
66   printf("  char:      %d (%p %p %p)\n",
67     (char*)&c1  - (char*)&c2  - sizeof(char),       &c1,  &ch0, &c2);
68   printf("  short:     %d (%p %p %p)\n",
69     (char*)&s1  - (char*)&s2  - sizeof(short),      &s1,  &ch3, &s2);
70   printf("  int:       %d (%p %p %p)\n",
71     (char*)&i1  - (char*)&i2  - sizeof(int),        &i1,  &ch4, &i2);
72   printf("  long:      %d (%p %p %p)\n",
73     (char*)&l1  - (char*)&l2  - sizeof(long),       &l1,  &ch7, &l2);
74   printf("  long long: %d (%p %p %p)\n",
75     (char*)&ll1 - (char*)&ll2 - sizeof(long long),  &ll1, &ch5, &ll2);
76   printf("  float:     %d (%p %p %p)\n",
77     (char*)&f1  - (char*)&f2  - sizeof(float),      &f1,  &ch2, &f2);
78   printf("  double:    %d (%p %p %p)\n",
79     (char*)&d1  - (char*)&d2  - sizeof(double),     &d1,  &ch1, &d2);
80   printf("  int*:      %d (%p %p %p)\n",
81     (char*)&p1  - (char*)&p2  - sizeof(int*),       &p1,  &ch6, &p2);
82   printf("structure alignments:\n");
83   printf("  char struct:      %d (%p %p %p)\n",
84     (char*)&sc1  - (char*)&sc2  - sizeof(s_char),   &sc1,  &ch8, &sc2);
85   printf(" short struct:      %d (%p %p %p)\n",
86     (char*)&ss1  - (char*)&ss2  - sizeof(s_short),  &ss1,  &ch9, &ss2);
87   printf("   int struct:      %d (%p %p %p)\n",
88     (char*)&si1  - (char*)&si2  - sizeof(s_int),    &si1,  &ch10,&si2);
89   printf("  long struct:      %d (%p %p %p)\n",
90     (char*)&sl1  - (char*)&sl2  - sizeof(s_long),   &sl1,  &ch11,&sl2);
91   printf(" llong struct:      %d (%p %p %p)\n",
92     (char*)&sll1  - (char*)&sll2  - sizeof(s_long_long),&sll1,&ch12,&sll2);
93   printf(" float struct:      %d (%p %p %p)\n",
94     (char*)&sf1  - (char*)&sf2  - sizeof(s_float),  &sf1,  &ch13,&sf2);
95   printf("double struct:      %d (%p %p %p)\n",
96     (char*)&sd1  - (char*)&sd2  - sizeof(s_double), &sd1,  &ch14,&sd2);
97   printf("  int* struct:      %d (%p %p %p)\n",
98     (char*)&sp1  - (char*)&sp2  - sizeof(s_intp),   &sp1,  &ch15,&sp2);
99
100   return 0;
101 }