`sizeof` 在未实现的函数上如何工作?

huangapple go评论48阅读模式
英文:

how sizeof works on unimplemented function?

问题

#include <stdio.h>

int hello();
char world();

int main() {
  printf("sizeof hello = %zu, sizeof world = %zu\n", sizeof(hello()), sizeof(world()));
}
sizeof hello = 4, sizeof world = 1
英文:

I have two functions without any implementation.

I expect that the linker returns an undefined reference to hello and world error.

But surprisingly, the code compiles and runs without any error.

#include &lt;stdio.h&gt;

int hello();
char world();

int main() {
  printf(&quot;sizeof hello = %zu, sizeof world = %zu\n&quot;, sizeof(hello()), sizeof(world()));
}
sizeof hello = 4, sizeof world = 1

答案1

得分: 12

sizeof(hello()) 的大小是返回类型 int 的大小,而不是函数的大小。函数没有被调用。

不需要定义函数来确定其返回类型,它由 int hello(); 声明。

更深入(在C中)

sizeofsizeof unary-expressionsizeof ( type-name ) 一起使用。

大小是从操作数的类型确定的。结果是一个整数。如果操作数的类型是可变长度数组类型,则会计算操作数;否则,不会计算操作数,结果是一个整数常数。C17dr § 6.5.3.4 2

由于操作数 hello() 的类型是 int(而不是可变长度数组),因此不会计算操作数,就像是 sizeof(int) 一样。

此外:sizeof 返回一个 size_t

"%zu" 是用来匹配 size_t 的正确格式说明符。"%ld" 未指定可以使用。

英文:

sizeof(hello()) is the size of the the return type, int, not the size of the function. The function is not called.

The function does not need to be defined to determine its return type declared by int hello();.


Deeper (in C)

sizeof works with sizeof unary-expression and sizeof ( type-name ).

> 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. C17dr § 6.5.3.4 2

Since the type of the operand hello() is an int (and not a variable length array), the operand is not evaluated and is then like sizeof(int).


Aside: sizeof returns a size_t.

&quot;%zu&quot; is a correct specifier to match a size_t. &quot;%ld&quot; is not specified to work.

// printf(&quot;sizeof hello = %ld, sizeof world = %ld\n&quot;, sizeof(hello()), sizeof(world()));
printf(&quot;sizeof hello = %zu, sizeof world = %zu\n&quot;, sizeof(hello()), sizeof(world()));

答案2

得分: 3

sizeof是未计算的上下文。它实际上不调用函数。不需要定义。声明足以知道它们返回intchar

你也可以写成sizeof(int)sizeof(char)来获得相同的输出。sizeof(&amp;hello)将导致函数指针到hello的大小,也不需要定义。而sizeof(hello)根本没有意义,因为函数没有大小(至少不是sizeof可以告诉你的那种意义)。

有关详细信息,我参考您到https://en.cppreference.com/w/cpp/language/sizeof(C++)和https://en.cppreference.com/w/c/language/sizeof(C)。

英文:

sizeof is unevaluated context. It does not actually call the functions. No definition is required. The declaration is sufficient to know that they return int and char.

You could as well have written sizeof(int) and sizeof(char) to get the same output. sizeof(&amp;hello) would result in the size of a function pointer to hello and does not require the definition either. And sizeof(hello) just makes no sense, because functions have no size (at least not in a sense that sizeof could tell you).

For details I refer you to https://en.cppreference.com/w/cpp/language/sizeof (c++) and https://en.cppreference.com/w/c/language/sizeof (c).

答案3

得分: 1

你无法像对int类型的变量使用sizeof()一样,对未实现的函数使用sizeof()来确定函数实现使用了多少内存。根据C11标准的6.5.3.4 sizeof和_Alignof运算符,第1段

不得对具有函数类型的表达式应用sizeof运算符...

draft C23标准中有相同的措辞:

不得对具有函数类型的表达式应用sizeof运算符...

英文:

The title of your question:

> how sizeof works on unimplemented function?

implies you are trying to determine how much memory the function implementation uses, similar to sizeof() int returning how much memory an int variable uses.

You can't use sizeof() on a function like that.

Per 6.5.3.4 The sizeof and _Alignof operators, paragraph 1 of the (draft) C11 standard:

> The sizeof operator shall not be applied to an expression that has function type...

The draft C23 standard has the exact same wording:

> The sizeof operator shall not be applied to an expression that has function type...

huangapple
  • 本文由 发表于 2023年2月8日 20:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定