我是否投射 malloc 的结果? [英] Do I cast the result of malloc?

查看:20
本文介绍了我是否投射 malloc 的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题中,有人在评论中建议我应该转换malloc的结果.即,我应该这样做:

In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this:

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

而不是:

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

为什么会这样?

推荐答案

TL;DR

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

有两个问题.强制转换以及您使用类型而不是变量作为 sizeof 的参数.相反,请这样做:

has two problems. The cast and that you're using the type instead of variable as argument for sizeof. Instead, do like this:

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

长版

;你不要转换结果,因为:

  • 这是不必要的,因为在这种情况下 void * 会自动且安全地提升为任何其他指针类型.
  • 它增加了代码的混乱,转换不是很容易阅读(尤其是当指针类型很长时).
  • 这会让你重复自己,这通常是不好的.
  • 如果您忘记包含 ,它可以隐藏错误.这可能会导致崩溃(或者,更糟糕的是,不会 导致崩溃,直到稍后在代码的某些完全不同的部分发生).考虑如果指针和整数的大小不同会发生什么;那么您通过投射隐藏了警告,并且可能会丢失您返回的地址的一部分.注意:从 C99 开始,隐式函数从 C 中消失了,这点不再相关,因为没有自动假设未声明的函数返回 int.
  • It is unnecessary, as void * is automatically and safely promoted to any other pointer type in this case.
  • 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>. 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.

作为澄清,请注意我说的是你不投",而不是你不需要需要投".在我看来,即使你做对了,包括演员也是失败的.这样做并没有什么好处,但有一堆潜在的风险,包括演员表表明你不知道这些风险.

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.

另外请注意,正如评论员指出的那样,上面讨论的是直接的 C,而不是 C++.我非常坚信 C 和 C++ 是独立的语言.

Also note, as commentators point out, that the above talks about straight C, not C++. I very firmly believe in C and C++ as separate languages.

更进一步,您的代码会不必要地重复可能导致错误的类型信息 (int).最好取消引用用于存储返回值的指针,以锁定"指针.两个在一起:

To add further, your code needlessly repeats the type information (int) which can cause errors. 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);

这也将 length 移到前面以增加可见性,并删除带有 sizeof 的冗余括号;它们只有在参数是类型名称时才需要.许多人似乎不知道(或忽略)这一点,这使得他们的代码更加冗长.记住:sizeof 不是函数!:)

This also moves the length to the front for increased visibility, and drops the redundant parentheses with 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! :)

虽然将length移到前面可能在一些罕见的情况下增加可见性,但也应该注意,在一般情况下,最好写下表达式如:

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:

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

因为首先保持 sizeof,在这种情况下,确保乘法至少用 size_t 数学完成.

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

比较:malloc(sizeof *sieve * length * width) vs. malloc(length * width * sizeof *sieve) 第二个可能溢出length* widthwidthlength 是小于 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 的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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