“sizeof(函数名)”是什么意思?返回?

发布于 2024-08-29 12:48:52 字数 166 浏览 12 评论 0原文

示例代码:

int main(void)
{
    printf ("size = %d\n", sizeof(main));
}

应用于函数名称(例如 main)的返回值 sizeof 是多少?

Sample code:

int main(void)
{
    printf ("size = %d\n", sizeof(main));
}

What is the returned value sizeof applied to a function name, for example main?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤凫 2024-09-05 12:48:52

C 标准禁止它 - 当使用 gcc -pedantic 编译时,它会产生 invalid application of 'sizeof' to a function type 警告。

然而,gcc编译它并为sizeof(main)返回1,并且它不是函数指针的大小。

它似乎与编译器相关。

C standard forbids it - when compiled with gcc -pedantic, it produces invalid application of ‘sizeof’ to a function type warning.

However gcc compiles it and returns 1 for sizeof(main), and it is not a size of function pointer.

It seems to be compiler-dependent.

红焚 2024-09-05 12:48:52

运算符的大小< /h3>

 一元表达式的大小
 sizeof ( 类型名称 )

操作数可以是一元表达式的标识符,也可以是类型转换表达式(即括在括号中的类型说明符)。 一元表达式不能表示位域对象、不完整类型或函数指示符。结果是一个无符号整数常量。标准标头 STDDEF.H 将此类型定义为 size_t

使用编译标志 -Wall -pedanticsizeof 发出有关错误操作数的警告(记住 sizeof 是编译时运算符),代码:

$ cat sizeof.c 
#include<stdio.h> 
int main(){
    printf("%zu %p\n", sizeof(main), (void*)main);
    return 0;
} 

编译消息使用 GCC 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5):

$ gcc -Wall -pedantic sizeof.c -std=c99
sizeof.c: In function ‘main’:
sizeof.c:3:30: warning: invalid application of ‘sizeof’ to a function type 
              [-pedantic]
sizeof.c:3:38: warning: ISO C forbids conversion of function pointer to 
              object pointer type [-pedantic]

另请阅读:

6.5.3.4 sizeof 运算符

1118 — sizeof 运算符不能应用于具有函数类型或不完整类型的表达式、此类类型的括号名称或指定位字段成员的表达式
1127 — 结果的值是实现定义的,其类型(无符号整数类型)是 size_t,在 (以及其他标头)中定义)

此外,size_t 的正确格式字符串是 %zu,如果它不存在,例如 Microsoft 的编译器,那么您可以使用 %lu 和将返回值转换为unsigned long

The sizeof Operator

 sizeof unary-expression
 sizeof ( type-name )

The operand is either an identifier that is a unary-expression, or a type-cast expression (that is, a type specifier enclosed in parentheses). The unary-expression cannot represent a bit-field object, an incomplete type, or a function designator. The result is an unsigned integral constant. The standard header STDDEF.H defines this type as size_t.

Use compilation flag -Wall -pedantic to emit warning about incorrect operand to sizeof (remember sizeof is a compilation time operator), Code:

$ cat sizeof.c 
#include<stdio.h> 
int main(){
    printf("%zu %p\n", sizeof(main), (void*)main);
    return 0;
} 

Compilation message with GCC version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5):

$ gcc -Wall -pedantic sizeof.c -std=c99
sizeof.c: In function ‘main’:
sizeof.c:3:30: warning: invalid application of ‘sizeof’ to a function type 
              [-pedantic]
sizeof.c:3:38: warning: ISO C forbids conversion of function pointer to 
              object pointer type [-pedantic]

Read also:

6.5.3.4 The sizeof operator

1118 — The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member
1127 — The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers)

Additionally, correct format string for size_t is %zu and If it doesn't exists for example Microsoft's compiler then you can use %lu and convert the returned value to unsigned long.

天荒地未老 2024-09-05 12:48:52

ISO C++ 禁止将 sizeof 应用于函数类型的表达式。

ISO/IEC 14882 on C++ 规定(第 5.3.3 节):

“大小运算符不得应用于具有函数或不完整类型的表达式,...”

对于标准 C ( ISO/IEC 9899:1999) 第 6.5.3.4 节:

sizeof 运算符不得应用于具有函数类型或不完整类型的表达式、此类类型的括号名称或指定的表达式位字段成员。”

ISO C++ forbids applying sizeof to an expression of function type.

ISO/IEC 14882 on C++ says (section 5.3.3):

"The size operator shall not be applied to an expression that has function or incomplete type,..."

The same hold for standard C (ISO/IEC 9899:1999) section 6.5.3.4:

"The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member."

み青杉依旧 2024-09-05 12:48:52

根据 ISO C11 第 6.5.3.4 The sizeof 和 _Alignof 运算符 第 1 小节(约束):

sizeof 运算符不得应用于具有函数类型或不完整类型的表达式、此类带括号的名称类型,或指定位字段成员的表达式。

6.3.2.1 左值、数组和函数指示符 中还有第 4 小节,其中指出:

函数指示符是具有函数类型的表达式。除非它是 sizeof 运算符、_Alignof 运算符(65) 或一元 & 的操作数运算符,类型为“函数返回”的函数指示符
type”被转换为具有类型“指向函数返回的指针”的表达式
输入“”。

引用自那里的脚注 65 澄清了这一点:

由于不会发生此转换,因此 sizeof_Alignof 运算符的操作数仍然是函数指示符,并且违反了 6.5.3.4 中的约束。

根据 4 一致性 部分:

在本国际标准中,“应”应解释为对实施或程序的要求;相反,“不得”应被解释为禁止。

因此,严格遵守的程序不应采用函数的大小。但是,话又说回来,它可能还应该使用正确的 main() 形式:-)

但是,这里有一个漏洞。允许一致的实现提供扩展“只要它们不改变任何严格的行为”
符合程序”(4 一致性 部分,6 小节)。

您可能会认为这是一种行为改变(允许 sizeof(function ) 而不是不允许它)但是,由于原始程序一开始就不会严格遵守,因此本小节并不禁止它。

As per ISO C11 section 6.5.3.4 The sizeof and _Alignof operators, sub-section 1 (constraints):

The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

There's also a subsection 4 in section 6.3.2.1 Lvalues, arrays, and function designators which states:

A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the _Alignof operator(65) or the unary & operator, a function designator with type "function returning
type" is converted to an expression that has type "pointer to function returning
type"".

Foot note 65, referenced from there, clarifies the point:

Because this conversion does not occur, the operand of the sizeof or _Alignof operator remains a function designator and violates the constraints in 6.5.3.4.

As per section 4 Conformance:

In this International Standard, "shall" is to be interpreted as a requirement on an implementation or on a program; conversely, "shall not" is to be interpreted as a prohibition.

Hence a strictly conforming program should never take the size of a function. But, then again, it probably should also use a correct form of main() :-)

There is a loophole here, however. A conforming implementation is allowed to provide extensions "provided they do not alter the behavior of any strictly
conforming program" (section 4 Conformance, subsection 6).

You could argue that this is a behavioural change (allowing sizeof(function) rather than disallowing it) but, since the original program wouldn't have been strictly conforming in the first place, the subsection does not forbid it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文