简体   繁体   English

在这种情况下,malloc做了什么?

[英]What does malloc do in this situation?

What does this line of C code do? 这行C代码有什么作用?

      be_node *ret = malloc(sizeof(*ret));

The definition of be_node can be found in this file: http://funzix.git.sourceforge.net/git/gitweb.cgi?p=funzix/funzix;a=blob_plain;f=bencode/bencode.h;hb=HEAD be_node的定义可以在这个文件中找到: http ://funzix.git.sourceforge.net/git/gitweb.cgi?p = funzix / funzix; a = blob_plain; f = bencode / bencode.h; hb = HEAD

The line of code above was found in this file: http://funzix.git.sourceforge.net/git/gitweb.cgi?p=funzix/funzix;a=blob_plain;f=bencode/bencode.c;hb=HEAD 上面的代码行在此文件中找到: http//funzix.git.sourceforge.net/git/gitweb.cgi?p = funzix / funzix; a = blob_plain; f = bencode / bencode.c; hb = HEAD

I don't understand what the sizeof(*ret) would return if it has only just been declared? 我不明白sizeof(* ret)如果刚刚被宣布会返回什么?

It's no different to any other use of sizeof ; 它与sizeof任何其他用法没有什么不同; it will evaluate the size of its operand. 它将评估其操作数的大小。 sizeof is based on compile-time information, 1 so it doesn't matter that ret has only just been declared. sizeof基于编译时信息, 1因此,刚刚声明了ret并不重要。

This idiom is the preferred way of using malloc . 这个成语是使用malloc的首选方式。 If you were to use be_node *ret = malloc(sizeof(be_node)) , then consider what would happen if you change the type of ret at a later date. 如果您要使用be_node *ret = malloc(sizeof(be_node)) ,那么请考虑如果您在以后更改ret类型会发生什么。 If you forget to replace both uses of " be_node ", then you will have introduced a subtle bug. 如果您忘记替换“ be_node ”的两种用法,那么您将引入一个微妙的错误。


1. Except in the case of variable-length arrays. 1.除了可变长度数组的情况。

sizeof(*ret) is resolved by the compiler and only looks at the type of *ret , not its contents. sizeof(*ret)由编译器解析,只查看*ret的类型,而不是其内容。 In this case, it's the size of a be_node. 在这种情况下,它是be_node的大小。 It also gets resolved at compile time, not at runtime so it doesn't "return" per se, it just gets substituted with a constant. 它也会在编译时解析,而不是在运行时解析,因此它本身不会“返回”,它只会被常量替换。

The compiler is going to take out sizeof(*ret) and substitute in a constant number that is the size of the be_node in bytes. 编译器将取出sizeof(*ret)并替换为be_node大小的常量数字(以字节为单位)。

It's a shortcut, or whatever you might want to call it. 这是一条捷径,或者你可能想要的任何东西。

You can either write 你可以写

be_node *ret = malloc(sizeof(be_node));

or 要么

be_node *ret = malloc(sizeof(*ret));

In the first case, you're basically saying "allocate a memory block large enough to hold a be_node". 在第一种情况下,你基本上是在说“分配足够大的内存块以容纳be_node”。 In the second case you're saying "allocate a memory block large enough to hold whatever ret points to". 在第二种情况下,你说“分配一个足够大的内存块来保存任何转发点”。 Which one you prefer is mostly a matter of preference. 您更喜欢哪一个主要是偏好问题。

sizeof works with both data types and actual variables. sizeof适用于数据类型和实际变量。 In your case, you invoke it with a variable as a parameter. 在您的情况下,您使用变量作为参数调用它。 By the time you invoke sizeof the variable is DECLARED (not initialized but declared) so it will know the var's type and will be able to compute the memory requirements in bytes which will be returned and used by the malloc. 当你调用sizeof时,变量是DECLARED(未初始化但声明),因此它将知道var的类型,并且能够以字节为单位计算内存需求,这些内容将由malloc返回和使用。

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

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