非常迂腐,但是...... [英] Extremely pedantic, but however...

查看:75
本文介绍了非常迂腐,但是......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



允许免费是不是效率低下接受空指针?将

禁止它并提供一个辅助功能

,如果它需要的时候:


#define FREE_SAFE(p)((void)((p)&& free((p))))


-


Frederick Gotham


Is it not inefficient to have allowed "free" to accept a null pointer? Would
it not have been better to disallow it, and to provide an auxiliary function
for times when its wanted:

#define FREE_SAFE(p) ( (void)( (p) && free((p)) ) )

--

Frederick Gotham

推荐答案



" Frederick Gotham" < fg ******* @ SPAM.com在留言中写道

新闻:W1 ******************* @ news.indigo .ie ...

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:W1*******************@news.indigo.ie...

>

允许免费是否效率低下接受一个空指针?



禁止它并没有更好,并提供一个辅助的

功能
需要时间



#define FREE_SAFE(p)((void)((p)&& free((p)))) />
>
Is it not inefficient to have allowed "free" to accept a null pointer?
Would
it not have been better to disallow it, and to provide an auxiliary
function
for times when its wanted:

#define FREE_SAFE(p) ( (void)( (p) && free((p)) ) )



在过去的日子里,释放一个空指针会使你的计算机崩溃。

开销可能是微不足道的,方便是值得的。

如果请求的大小为零的区域是
,则允许malloc()返回null。所以免费null并不一定是非法的。

-
www.personal.leeds.ac.uk/~bgy1mm

免费下载游戏。

In the olden days freeing a null pointer would crash your computer.
The overhead is probably so trivial that the convenience is worth it.
Plus malloc() is allowed to return null if a region of zero size is
requested. So it is not necessarily illegitimate to free null.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


Frederick Gotham写道:
Frederick Gotham wrote:

允许免费是否效率低下接受空指针?
Is it not inefficient to have allowed "free" to accept a null pointer?



仅限C语言。其他语言的程序员通常不关心

无关紧要的额外比较。 (free()本身在大多数

实现中并不便宜。)

Only in C. Programmers in other languages generally don''t care about an
insignificant extra comparison. (free() itself is hardly cheap on most
implementations.)


拒绝它并提供它并不是更好需要时的辅助

函数:
Would it not have been better to disallow it, and to provide an auxiliary
function for times when its wanted:



定义更好。 C已经因为没有检查他们的参数(或者在某些情况下无法检查他们的参数中的
)的标准

函数而引发了很多抨击。确保免费(NULL)

相对较小的成本并不值得(请注意,在一般情况下free()不会

有可观察到的副作用,所以免费(NULL)无操作不会降低

可靠性。


S.

Define "better". C already catches a lot of flak for having standard
functions that do not check their arguments (or in some cases cannot check
their arguments). The comparatively small cost of ensuring free(NULL)
doesn''t crash is worth it (note that in the general case free() does not
have observable side effects, so making free(NULL) a no-op does not decrease
reliability).

S.

Skarmander发布:
Skarmander posted:

>允许免费是否效率不高?接受空指针?
>Is it not inefficient to have allowed "free" to accept a null pointer?



仅限于C.其他语言的程序员通常不关心

无关紧要的额外比较。 (free()本身在大多数

实现上都不便宜。)


Only in C. Programmers in other languages generally don''t care about an
insignificant extra comparison. (free() itself is hardly cheap on most
implementations.)



我不是在谈论程序员,我是谈论电脑运行

节目。如果特定机器需要20纳秒来检查

指针是否为空,那么每次调用时算法的执行时间会延长

20纳秒"游离QUOT; (如果这些null

检查中的每一个都是冗余的)。如果

免费,这20纳秒就可以被收回。没有检查空指针。


一个特定的人(也就是程序员,在这种情况下)

认为20纳秒可以忽略不计是无关紧要的我的查询。


I''m not talking about programmers, I''m talking about computers running
programs. If it takes 20 nanoseconds for a particular machine to check if a
pointer is null, then the execution time of your algorithm is extended by
20 nanoseconds for each time you call "free" (if every one of these null
checks is redundan). Those 20 nanoseconds could have been reclaimed if
"free" didn''t check for null pointers.

Whether a particular human being (aka programmer, in this context)
considers 20 nanoseconds to be negligible is irrelevant to my query.


>禁止它并提供辅助函数适用于其所需的时间:
>Would it not have been better to disallow it, and to provide an
auxiliary function for times when its wanted:



定义更好。


Define "better".



效率更高。运行得更快。使用较少的资源。


More efficient. Runs faster. Uses less resources.


C已经因为没有检查其参数的标准

函数而引发了很多抨击(或者在某些情况下不能检查他们的论点)。
C already catches a lot of flak for having standard
functions that do not check their arguments (or in some cases cannot
check their arguments).



取决于你的要求。我祝贺C的效率。如果您想要持有

手,您可以获得一个包装库:


size_t strlen_HOLD_MY_HAND(char const * const p)

{

如果(p)返回strlen(p);


返回0;

}


Depends who you ask. I congratulate C for its efficiency. If you want your
hand to be held, you could get a wrapper library:

size_t strlen_HOLD_MY_HAND(char const *const p)
{
if (p) return strlen(p);

return 0;
}


确保

免费(NULL)不会崩溃的相对较小的成本是值得的(请注意,在一般情况下

free()没有可观察到的副作用,因此免费(NULL)a

no-op不会降低可靠性)。
The comparatively small cost of ensuring
free(NULL) doesn''t crash is worth it (note that in the general case
free() does not have observable side effects, so making free(NULL) a
no-op does not decrease reliability).



如果你想要能够调用免费的功能在空指针上,

然后所需要的只是简单的:


#define FREE_SAFE(p)do {void * const q = p; if(q)free(q); } while(0);


这样,您可以使用免费无论何时空检查都是多余的,

在执行时间内节省20纳秒。


-


弗雷德里克Gotham


If you want the feature of being able to invoke "free" upon a null pointer,
then all it takes is something simple like:

#define FREE_SAFE(p) do { void *const q = p; if(q) free(q); } while (0);

This way, you can use "free" whenever a null check would be redundant,
saving 20 nanoseconds in execution time.

--

Frederick Gotham


这篇关于非常迂腐,但是......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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