简体   繁体   English

每个字段的偏移量和以下结构声明的大小

[英]The offset of each field and the size of the following structure declarations

A. struct P1 {short i; int c; int *j; short *d;}; 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]}; 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}; 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. 答案说P1的总大小是16个字节。 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? 但我认为短路需要4个(插入2个字节以满足对齐要求),int需要4个,两个指针* j和* d每个需要8个。所以总大小应该是4 + 4 + 8 + 8 = 24。搞错了? Besides, for the E. P5, the offset of t is 24. I don't know how it comes. 此外,对于E. P5,t的偏移是24.我不知道它是怎么来的。 a[2] is an array with two elements. a [2]是一个包含两个元素的数组。 Each element is a P4 structure. 每个元素都是P4结构。 Since the size of P4 is 32, shouldn't a[2] take 64 bytes? 既然P4的大小是32,那么[2]不应该占用64个字节?

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? 所以总大小应该是4 + 4 + 8 + 8 = 24.我弄错了吗?

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? 既然P4的大小是32,那么[2]不应该占用64个字节?

Like the prior question, many considerations are in play. 与先前的问题一样,许多考虑因素都在起作用。


To find the offset of a member in a struct , use offsetof() 要查找struct成员的偏移量,请使用offsetof()

To find the size of a struct , use sizeof() . 要查找struct的大小,请使用sizeof()

To print these values, use the correct matching print specifier: "%zu" 要打印这些值,请使用正确的匹配打印说明符: "%zu"

Use proper syntax. 使用正确的语法。 Review use of ; 审查使用; in P4 and P5 . P4P5


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. 但是,由于原始类型和指针(如intshort*未链接到特定大小,因此结果可能因平台而异。 I ran it on Windows Subsystem for Linux and my output was: 我在Windows子系统Linux上运行它,我的输出是:

$ 24 32 88

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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