关于函数的大小 [英] Regarding the sizeof a function

查看:57
本文介绍了关于函数的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我写了一个程序


void main()

{

printf("%d%d \ n",sizeof main,sizeof(main()));

}


在这个程序中输出是1和4,


我从书中理解,函数名称代表函数的地址

。我认为sizeof main应该给4,而且我不知道
知道为什么sizeof(main())给4,有人可以解释一下

前者一个人做了什么,后者做了什么?为什么会出现这种情况?


感谢你在这方面的帮助。


谢谢,

Vikas。

Hi,

I have written a program

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

in this program the output is 1 and 4,

as i understood from the books, function name represents the address
of the function. i thought sizeof main should give 4 , and i don''t
know why sizeof(main()) is giving 4, Can some one please explain what
the former one does and what the later one do?. Why is this behavior?

Appreciate your help in this regard.

Thanks,
Vikas.

推荐答案

在文章< 11 ********************** @ a26g2000pre.googlegroups .com>,

venkat< ve ********** @ gmail.comwrote:
In article <11**********************@a26g2000pre.googlegroups .com>,
venkat <ve**********@gmail.comwrote:

printf("%d%d \ n",sizeof main,sizeof(main()));
printf("%d %d\n", sizeof main, sizeof(main()));



程序中还有其他几个错误,但是


(a)当<时,函数不会转换为函数指针br />
用作sizeof的操作数。将

sizeof应用于某个函数是违法的。


(b)将sizeof应用于void是违法的,因为它'' s $

不完整类型。


- Richard

-

代价应在一些字母表中需要多达32个字符

" - 1963年的X3.4。

There are several other errors in your program, but

(a) Functions are not converted to function pointers when
used as the operand of sizeof. It''s illegal to apply
sizeof to a function.

(b) It''s illegal to apply sizeof to void, since it''s an
incomplete type.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


6月15日下午4:29,富... @ cogsci.ed.ac.uk(Richard Tobin)写道:
On Jun 15, 4:29 pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:

文章< 1181906769.365502.273 ... @ a26g2000pre.googlegroups .com>,


venkat< venkatavi。 .. @ gmail.comwrote:
In article <1181906769.365502.273...@a26g2000pre.googlegroups .com>,

venkat <venkatavi...@gmail.comwrote:

printf("%d%d \ n",sizeof main,sizeof(main()));
printf("%d %d\n", sizeof main, sizeof(main()));



您的程序中还有其他几个错误,但是


(a)当<时,函数不会转换为函数指针br />
用作sizeof的操作数。将

sizeof应用于某个函数是违法的。


(b)将sizeof应用于void是违法的,因为它'' s $

不完整类型。


- Richard

-

代价应在一些字母表中需要多达32个字符

" - X3.4,1963。


There are several other errors in your program, but

(a) Functions are not converted to function pointers when
used as the operand of sizeof. It''s illegal to apply
sizeof to a function.

(b) It''s illegal to apply sizeof to void, since it''s an
incomplete type.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.



理查德,


感谢您的回复,我没有观察到那么多,我想避免

返回,这就是为什么我把虚拟程序放在程序中,程序是给

以下


main()

{

printf("%d%d \ n",sizeof main,sizeof(main()));


返回;


}

Hi Richard,

Thanks for replying, i haven''t observed that much, i want to avoid the
return that is why i put void in the program , the program is give
below

main()
{
printf("%d %d\n", sizeof main, sizeof(main()));

return;

}


venkat说:

< snip>
venkat said:

<snip>

感谢您的回复,我没有观察到那么多,我想避免

返回这就是为什么我把void放在程序中,
Thanks for replying, i haven''t observed that much, i want to avoid the
return that is why i put void in the program ,



但是通过这样做,你不再用C语言了。

But by doing so, you are no longer writing in C.


程序在下面给出


main()

{

printf("%d %d \ n",sizeof main,sizeof(main()));
the program is give below

main()
{
printf("%d %d\n", sizeof main, sizeof(main()));



未定义的行为,因为您在范围内调用了一个没有有效

原型的可变参数函数。解决方案:#include< stdio.h>


未定义的行为,因为当它'

期待int时,你将size_t传递给printf。你这样做了两次。解决方案:将强制转换添加到int。 (罕见的

正确使用演员表。)


因为你需要诊断信息而违反约束条件

尝试将sizeof应用于具有函数类型的表达式。解决方案:

不要将sizeof应用于具有功能类型的表达式。


-

Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Undefined behaviour because you call a variadic function without a valid
prototype in scope. Solution: #include <stdio.h>

Undefined behaviour because you''re passing size_t to printf when it''s
expecting int. You do this twice. Solution: add casts to int. (A rare
correct use of a cast.)

Constraint violation requiring a diagnostic message because you''re
trying to apply sizeof to an expression having function type. Solution:
don''t apply sizeof to an expression having function type.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于关于函数的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆