Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

c - 我要转换malloc的结果吗?(Do I cast the result of malloc?)

In this question , someone suggested in a comment that I should not cast the result of malloc , ie

(在这个问题 ,有人建议意见 ,我应该投的结果malloc ,即)

int *sieve = malloc(sizeof(int) * length);

rather than:

(而不是:)

int *sieve = (int *) malloc(sizeof(int) * length);

Why would this be the case?

(为什么会这样呢?)

  ask by Patrick McDonald translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No ;

(没有)

you don't cast the result, since:

(您投放结果,因为:)

  • It is unnecessary, as void * is automatically and safely promoted to any other pointer type in this case.

    (这是不必要的,因为在这种情况下, void *会自动安全地提升为任何其他指针类型。)

  • It adds clutter to the code, casts are not very easy to read (especially if the pointer type is long).

    (它给代码增加了混乱,强制转换不是很容易阅读(特别是如果指针类型很长的话)。)

  • It makes you repeat yourself, which is generally bad.

    (它使您重复自己,这通常是不好的。)

  • It can hide an error if you forgot to include <stdlib.h> .

    (如果您忘记包含<stdlib.h>它可能会隐藏错误。)

    This can cause crashes (or, worse, not cause a crash until way later in some totally different part of the code).

    (这可能会导致崩溃(或更糟,而不是在代码中的一些完全不同的部分会导致崩溃,直到后来的方式)。)

    Consider what happens if pointers and integers are differently sized;

    (考虑一下如果指针和整数的大小不同会怎样?)

    then you're hiding a warning by casting and might lose bits of your returned address.

    (那么您将通过投射隐藏警告,并且可能会丢失返回地址的一部分。)

    Note: as of C99 implicit functions are gone from C, and this point is no longer relevant since there's no automatic assumption that undeclared functions return int .

    (注意:从C99开始,隐式函数不再使用C,这一点不再相关,因为没有自动假设未声明的函数返回int 。)

As a clarification, note that I said "you don't cast", not "you don't need to cast".

(为了澄清起见,请注意我说的是“您不要投”,而不是“您不需要投”。)

In my opinion, it's a failure to include the cast, even if you got it right.

(我认为,即使您没做错,也不能包括演员表。)

There are simply no benefits to doing it, but a bunch of potential risks, and including the cast indicates that you don't know about the risks.

(这样做没有任何好处,但是有很多潜在风险,包括强制转换说明您不知道这些风险。)

Also note, as commentators point out, that the above talks about straight C, not C++.

(还要注意,正如评论员指出的那样,以上内容是关于直接C而不是C ++的。)

I very firmly believe in C and C++ as separate languages.

(我非常坚信C和C ++是独立的语言。)

To add further, your code needlessly repeats the type information ( int ) which can cause errors.

(要进一步添加,您的代码不必要地重复可能导致错误的类型信息( int )。)

It's better to de-reference the pointer being used to store the return value, to "lock" the two together:

(最好取消引用用于存储返回值的指针,以将两者“锁定”在一起:)

int *sieve = malloc(length * sizeof *sieve);

This also moves the length to the front for increased visibility, and drops the redundant parentheses with sizeof ;

(这也将length移到前面以增加可见度,并用sizeof删除多余的括号;)

they are only needed when the argument is a type name.

(当参数为类型名称时才需要使用它们。)

Many people seem to not know (or ignore) this, which makes their code more verbose.

(许多人似乎不知道(或忽略)这一点,这会使他们的代码更加冗长。)

Remember: sizeof is not a function!

(请记住: sizeof不是函数!)

:)

(:))


While moving length to the front may increase visibility in some rare cases, one should also pay attention that in the general case, it should be better to write the expression as:

(虽然在某些极少数情况下将length移到最前面可能会增加可见性,但也应注意,在一般情况下,将表达式写为:)

int *sieve = malloc(sizeof *sieve * length);

Since keeping the sizeof first, in this case, ensures multiplication is done with at least size_t math.

(在这种情况下,由于先保留sizeof ,因此可以确保至少用size_t数学进行乘法。)

Compare: malloc(sizeof *sieve * length * width) vs. malloc(length * width * sizeof *sieve) the second may overflow the length * width when width and length are smaller types than size_t .

(比较: malloc(sizeof *sieve * length * width) vs. malloc(length * width * sizeof *sieve)widthlength是小于size_t类型时,秒可能会溢出length * width 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...