Systemnahe Programmierung in C: Ausrichtung im Speicher |
1#include <stdio.h>
2
3struct s1
4{
5 short f1;
6 long f2;
7 char f3;
8 long f4;
9 char f5;
10}
11r1;
12
13int
14main (void)
15{
16 printf ("sizeof(r1) : %d\n", sizeof (r1));
17 printf ("sizeof(r1.f1): %d\n", sizeof (r1.f1));
18 printf ("sizeof(r1.f2): %d\n", sizeof (r1.f2));
19 printf ("sizeof(r1.f3): %d\n", sizeof (r1.f3));
20 printf ("sizeof(r1.f4): %d\n", sizeof (r1.f4));
21 printf ("sizeof(r1.f5): %d\n", sizeof (r1.f5));
22
23 printf ("offset(f1): %d\n", (char *) (&r1.f1) - (char *) (&r1));
24 printf ("offset(f2): %d\n", (char *) (&r1.f2) - (char *) (&r1));
25 printf ("offset(f3): %d\n", (char *) (&r1.f3) - (char *) (&r1));
26 printf ("offset(f4): %d\n", (char *) (&r1.f4) - (char *) (&r1));
27 printf ("offset(f5): %d\n", (char *) (&r1.f5) - (char *) (&r1));
28
29 return 0;
30}
|
1#include <stdio.h>
2
3#ifdef __LP64__
4#define sizeofFormat "%lu"
5#define ptrdifFormat "%ld"
6#else
7#define sizeofFormat "%u"
8#define ptrdifFormat "%d"
9#endif
10
11struct s1
12{
13 short f1;
14 long f2;
15 char f3;
16 long f4;
17 char f5;
18}
19r1;
20
21int
22main (void)
23{
24 printf ("sizeof(r1) : " sizeofFormat "\n", sizeof (r1));
25 printf ("sizeof(r1.f1): " sizeofFormat "\n", sizeof (r1.f1));
26 printf ("sizeof(r1.f2): " sizeofFormat "\n", sizeof (r1.f2));
27 printf ("sizeof(r1.f3): " sizeofFormat "\n", sizeof (r1.f3));
28 printf ("sizeof(r1.f4): " sizeofFormat "\n", sizeof (r1.f4));
29 printf ("sizeof(r1.f5): " sizeofFormat "\n", sizeof (r1.f5));
30
31 printf ("offset(f1): " ptrdifFormat "\n", (char *) (&r1.f1) - (char *) (&r1));
32 printf ("offset(f2): " ptrdifFormat "\n", (char *) (&r1.f2) - (char *) (&r1));
33 printf ("offset(f3): " ptrdifFormat "\n", (char *) (&r1.f3) - (char *) (&r1));
34 printf ("offset(f4): " ptrdifFormat "\n", (char *) (&r1.f4) - (char *) (&r1));
35 printf ("offset(f5): " ptrdifFormat "\n", (char *) (&r1.f5) - (char *) (&r1));
36
37 return 0;
38}
|
1#include <stdio.h>
2
3#if ! defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
4
5this program assumes to be compiled with an ISO C99 compiler
6it uses %zd and %td format specifiers
7for gcc use -std=c99 option
8
9#endif
10
11struct s1
12{
13 short f1;
14 long f2;
15 char f3;
16 long f4;
17 char f5;
18}
19r1;
20
21int
22main (void)
23{
24 printf ("sizeof(r1) : %zd\n", sizeof (r1));
25 printf ("sizeof(r1.f1): %zd\n", sizeof (r1.f1));
26 printf ("sizeof(r1.f2): %zd\n", sizeof (r1.f2));
27 printf ("sizeof(r1.f3): %zd\n", sizeof (r1.f3));
28 printf ("sizeof(r1.f4): %zd\n", sizeof (r1.f4));
29 printf ("sizeof(r1.f5): %zd\n", sizeof (r1.f5));
30
31 printf ("offset(f1): %td\n", (char *) (&r1.f1) - (char *) (&r1));
32 printf ("offset(f2): %td\n", (char *) (&r1.f2) - (char *) (&r1));
33 printf ("offset(f3): %td\n", (char *) (&r1.f3) - (char *) (&r1));
34 printf ("offset(f4): %td\n", (char *) (&r1.f4) - (char *) (&r1));
35 printf ("offset(f5): %td\n", (char *) (&r1.f5) - (char *) (&r1));
36
37 return 0;
38}
|
1#include <stdio.h>
2
3#ifdef __LP64__
4#define sizeofFormat "%lu"
5#define ptrdifFormat "%ld"
6#else
7#define sizeofFormat "%u"
8#define ptrdifFormat "%d"
9#endif
10
11struct s1
12{
13 long f2;
14 long f4;
15 short f1;
16 char f3;
17 char f5;
18}
19r1;
20
21int
22main (void)
23{
24 printf ("sizeof(r1) : " sizeofFormat "\n", sizeof (r1));
25 printf ("sizeof(r1.f1): " sizeofFormat "\n", sizeof (r1.f1));
26 printf ("sizeof(r1.f2): " sizeofFormat "\n", sizeof (r1.f2));
27 printf ("sizeof(r1.f3): " sizeofFormat "\n", sizeof (r1.f3));
28 printf ("sizeof(r1.f4): " sizeofFormat "\n", sizeof (r1.f4));
29 printf ("sizeof(r1.f5): " sizeofFormat "\n", sizeof (r1.f5));
30
31 printf ("offset(f1): " ptrdifFormat "\n", (char *) (&r1.f1) - (char *) (&r1));
32 printf ("offset(f2): " ptrdifFormat "\n", (char *) (&r1.f2) - (char *) (&r1));
33 printf ("offset(f3): " ptrdifFormat "\n", (char *) (&r1.f3) - (char *) (&r1));
34 printf ("offset(f4): " ptrdifFormat "\n", (char *) (&r1.f4) - (char *) (&r1));
35 printf ("offset(f5): " ptrdifFormat "\n", (char *) (&r1.f5) - (char *) (&r1));
36
37 return 0;
38}
|
Letzte Änderung: 04.01.2016 | © Prof. Dr. Uwe Schmidt |