简体   繁体   English

在没有创建对象的情况下从指针中查找结构的正确大小?

[英]Finding the correct size of a struct from a pointer without creating an object?

Sorry if the title is confusing. 对不起,如果标题令人困惑。 Here's my struct : 这是我的结构

struct l_list{
   int number;
   char *name;
   double value;
   struct l_list *next;
};

typedef struct l_list *PhoneBook;

Main function: 主功能:

int main(void){

   printf("%u\n", sizeof(struct l_list));

   PhoneBook person1;

   printf("%u\n", sizeof(*person1));
   printf("%u\n", sizeof(PhoneBook));

   return 0;
}

The output is: 输出是:

20 20
20 20
4 4

I understand that PhoneBook is showing 4 bytes because it's only the size of the pointer , but how can you find the size of the actual struct from the typedef PhoneBook ? 据我所知, PhoneBook显示4个字节,因为它只是指针的大小,但是如何从typedef PhoneBook找到实际结构的大小?

You can use the following: 您可以使用以下内容:

printf("%zu\n", sizeof( *(PhoneBook)NULL ) );

the important question is why is the above valid, aren't you performing indirection on a NULL pointer which would be undefined behavior ? 重要的问题是为什么上面有效,是不是你在NULL 指针上执行间接 ,这将是未定义的行为 No, you are not since sizeof with the exception of variable length arrays is not evaluated at all. 不,你不是因为sizeof除了可变长度数组之外根本没有被评估。 If we look at the C99 draft standard section 6.5.3.4 The sizeof operator paragraph 2 says( emphasis mine ): 如果我们看一下C99标准草案 6.5.3.4操作员2段说明( 强调我的 ):

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. sizeof运算符产生其操作数的大小(以字节为单位),该操作数可以是表达式或类型的带括号的名称。 The size is determined from the type of the operand. 大小由操作数的类型确定。 The result is an integer. 结果是整数。 If the type of the operand is a variable length array type, the operand is evaluated; 如果操作数的类型是可变长度数组类型,则计算操作数; otherwise, the operand is not evaluated and the result is an integer constant. 否则,不评估操作数 ,结果是整数常量。

furthermore we see the following example in paragraph 5 which confirms this reading: 此外,我们在第5段中看到以下例子,证实了这一解读:

double *dp = alloc(sizeof *dp);

At compile time the type of the expression with be determined in order to compute the result. 在编译时,要确定表达式的类型以便计算结果。 We can further demonstrate this with the following example: 我们可以通过以下示例进一步证明这一点:

int x = 0 ;
printf("%zu\n", sizeof( x++ ));

which won't increment x . 这不会增加x

Note : %zu is a platform independent format specifier for the result of sizeof . 注意%zusizeof结果的独立平台的格式说明符。

你可以做:

printf("%u\n", sizeof(*(PhoneBook) NULL) );

I would do it like this: 我会这样做:

typedef struct l_list{
    int number;
    char *name;
    double value;
   struct l_list *next;
} PhoneBook, *PPhoneBook;

Then your PhoneBook is an typedef for struct l_list, rather than the pointer. 那么你的PhoneBook是struct l_list的typedef,而不是指针。 This makes more sense. 这更有意义。 PPhoneBook (or any other name of your choice) is typedef for Phonebook *. PPhoneBook(或您选择的任何其他名称)是Phonebook *的typedef。

Then you can do: 然后你可以这样做:

sizeof(PhoneBook)

and it will give you the correct size of a PhoneBook object. 它会为您提供正确的PhoneBook对象大小。

Or you can do: 或者你可以这样做:

sizeof(PPhoneBook)

and it will return 4 bytes, the pointer size. 它将返回4个字节,指针大小。 Note that hiding the fact that you're using a pointer behind a non-pointer-looking typedef (such as PPhoneBook) is not always advisable. 请注意,隐藏在非指针式typedef(例如PPhoneBook)后面使用指针的事实并不总是可取的。 It can make your code confusing. 它可能会使您的代码混乱。 However, if that's what you want, this is how to do it! 但是,如果那就是你想要的,那就是怎么做的!

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

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