简体   繁体   中英

The offset of each field and the size of the following structure declarations

A. struct P1 {short i; int c; int *j; short *d;}; struct P1 {short i; int c; int *j; short *d;};

D. struct P4 {char w[16]; int *c[2]}; struct P4 {char w[16]; int *c[2]};

E. struct P5 {struct P4 a[2]; struct P1 t}; struct P5 {struct P4 a[2]; struct P1 t};

The answer said the total size of P1 is 16 bytes. But I think the short takes 4 (insert 2 bytes to satisfy alignment requirement), int takes 4, two pointers *j and *d each takes 8. So the total size should be 4 + 4 + 8 + 8 = 24. Do I get it wrong? Besides, for the E. P5, the offset of t is 24. I don't know how it comes. a[2] is an array with two elements. Each element is a P4 structure. Since the size of P4 is 32, shouldn't a[2] take 64 bytes?

The offset of each field and the size of the following structure declarations

There are many issues of padding, alignment and integer sizes that affect the result. Best to use standard code to report the offset values and sizes.

So the total size should be 4 + 4 + 8 + 8 = 24. Do I get it wrong?

Reasonable calculation, yet not definitive. Size depends on alignment, padding of the compiler and platform. Even for a known architecture, the result may vary depending on the compiler and its options.

Since the size of P4 is 32, shouldn't a[2] take 64 bytes?

Like the prior question, many considerations are in play.


To find the offset of a member in a struct , use offsetof()

To find the size of a struct , use sizeof() .

To print these values, use the correct matching print specifier: "%zu"

Use proper syntax. Review use of ; in P4 and P5 .


struct P1 {
  short i;
  int c;
  int *j;
  short *d;
};

struct P4 {
  char w[16];
  int *c[2];
};

struct P5 {
  struct P4 a[2];
  struct P1 t;
};

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  printf("PI i:%zu\n", offsetof(struct P1, i));
  printf("PI c:%zu\n", offsetof(struct P1, c));
  printf("PI j:%zu\n", offsetof(struct P1, j));
  printf("PI d:%zu\n", offsetof(struct P1, d));
  printf("PI size:%zu\n\n", sizeof(struct P1));

  printf("P4 w:%zu\n", offsetof(struct P4, w));
  printf("P4 c:%zu\n", offsetof(struct P4, c));
  printf("P4 size:%zu\n\n", sizeof(struct P4));

  printf("P5 a:%zu\n", offsetof(struct P5, a));
  printf("P5 t:%zu\n", offsetof(struct P5, t));
  printf("P5 size:%zu\n\n", sizeof(struct P5));
}

Output: Your output may vary.

PI i:0
PI c:4
PI j:8
PI d:12
PI size:16

P4 w:0
P4 c:16
P4 size:24

P5 a:0
P5 t:48
P5 size:64

There's a simple way to find out a solution: write code and run it.

#include <stdio.h>

typedef struct P1 {
        short i;
        int c;
        int* j;
        short* d;
} P1_t;

typedef struct P2 {
        char w[16];
        int* c[2];
} P4_t;

typedef struct P5 {
        P4_t a[2];
        P1_t t;
} P5_t;

int main() {
        P1_t a;
        P4_t b;
        P5_t c;
        printf("%d %d %d\n", sizeof(a), sizeof(b), sizeof(c));
        return 0;
}

However, since primitive types and pointer like int and short* are not linked to a specific size, the results can vary depending on the platform. I ran it on Windows Subsystem for Linux and my output was:

$ 24 32 88

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM