Re: Re: Re: Re: Re: Re[2]: [cpp] cloudwu大哥,请解释一下网易互动笔试题目的意义所在&# 8230;&# 8230;

小陈 iamchenxin at 163.com
Thu Mar 23 19:54:23 CST 2006


 
 
哎,确实我这讨论有点穿牛角尖。这问题我在这回复之后就打住了,再说下去就罗嗦了,毕竟这问题结果是什么其实都没有多大价值  ^_^
在我看来 在参数里 允许传值 数组的存在,本身并不会干扰效率,相反是多给了程序员一种选择,程序员可以象使用其他东西一样在需要传值使用的时候传值使用,需要传址使用的时候传址使用(有些语言允许这样),程序员并不会因为 允许传值 数组的存在而永远坚持只使用传值形式的数组,如同一个class,既可以传值也可以传址,可是并不会因为class可以传值,程序员就只会传值了。
我看来导致数组只能传址的原因是sizeof int buf[]引起的,按照C/C++的编译模型,是永远也绕不过去的障碍。在这里效率并不是左右决定的关键,传值 数组的存在或者不存在,也不会对效率有任何的影响。expert c programming也只是简单的假设了一个希望传值的用户实际上并不会真的需要传值来论证传值影响了效率。
刚才去看了expert c programming ,恩,下面是书里的说明
&# 8220;The array/pointer equivalence for parameters was done for efficiency. All non-array data arguments in
C are passed "by value" (a copy of the argument is made and passed to the called function; the
function cannot change the value of the actual variable used as an argument, just the value of the copy
it has). However, it is potentially very expensive in memory and time to copy an array; and most of
the time, you don't actually want a copy of the array, you just want to indicate to the function which
particular array you are interested in at the moment. One way to do this might be to allow parameters
to have a storage specifier that says whether it is passed by value or by reference, as occurs in Pascal.
It simplifies the compiler if the convention is adopted that all arrays are passed as a pointer to the start,
and everything else is passed by making a copy of it. Similarly the return value of a function can never
be an array or function, only a pointer to an array or function.&# 8221;
 
 
 



-----原始邮件-----
发件人:"evilangel" 
发送时间:2006-03-23 17:21:53
收件人:"C++ Discuss Group" 
抄送:(无)
主题:Re: Re: Re: Re: Re[2]: [cpp] cloudwu大哥,请解释一下网易互动笔试题目的意义所在&# 8230;&# 8230;


小陈,您好!

C为什么把数组参数当成指针的原因在
<< Expert C Programming >> 里面的 "Why C Treats Array Parameters as Pointers"
里有很详细的说明..

当然你也可以说那是作者一厢情愿的借口而已...-_-!
	

>呵呵,可能是我钻牛角尖了,不过我还是觉得这是由于实质上 由于为了保持 int buf[] 和 int buf[5] 在sizeof下面保持的一致性才产生的结果。毕竟对于参数的int buf[] sizeof 在编译时是无法求值的(按照c++的单独编译模型无法求值)。其实在这里也是体现了语言的现实性,不然数组又怎么会无法引用? 如果不考虑编译器的局限性的话,我想c++ standard 未必会真的把参数 int buf[5] 给当成 指针,毕竟 参数 int buf[5] 在函数传值才是真的符合语义。如同其他类型一样,传入类型本身代表传值,不传值的话就要求程序员手动传入引用或者指针。 C和C++的口号向来是 要相信程序员,相信程序员明白自己在做什么。  说白了现在这样的规定是 由于 c++使用的单独编译模型的局限性造成的。 不然C++ 会不相信程序员要传入 int buf[5],是想进行传值, 会不相信程序员不知道不传值需要自己手动传指针和引用?按理说  int buf[5] 传值才是符合语言规范的。毕竟连class A{int buf[2222];};; 这种当参数传入时都是传值进入的。说是为了效率只是一厢情愿的借口。



        致
礼!
 				

        evilangel
        evilangel2000 at 163.com
          2006-03-23

_______________________________________________
Cpp mailing list
Cpp at codingnow.com
http://codingnow.com/mailman/listinfo/cpp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codingnow.com/pipermail/cpp/attachments/20060323/bcf0f95c/attachment.html


More information about the Cpp mailing list