问题描述
我只是读有关转换的malloc
的返回值的坏习惯。如果我理解正确的,这是绝对合法的离开,因为它是隐式进行(而且应该离开,因为它可能会产生其他问题,)的演员。那么我的问题是,我应该什么时候再投我的价值观?有一些一般规则或东西吗?例如,该code没有任何错误,编译与 GCC -W -Wall
(除未使用栏
,但这不是点):
I was just reading about the bad practice of casting the return value of malloc
. If I understood correctly, it is absolutely legal to leave the cast as it is done implicitly (and should be left, because of other problems it could generate). Well my question is, when should I then cast my values ? Is there some general rule or something ? For example, this code compiles without any errors with gcc -W -Wall
(except unused bar
, but that's not the point):
float foo(void) {
double bar = 4.2;
return bar;
}
int main(void) {
double bar = foo();
return 0;
}
我现在很困惑。什么是关于铸造良好做法和规则?
I'm confused now. What are the good practices and the rules about casting ?
感谢。
推荐答案
有需要完美地C.有效铸小心扫断言,如铸造总是不好的设计,因为他们显然和公然伪造的几种情况。
There are several situations that require perfectly valid casting in C. Beware of sweeping assertions like "casting is always bad design", since they are obviously and patently bogus.
一个巨大的情况下,组严格依赖于铸件的算术运算。铸造是在需要的情况下,当你需要强制从默认都不同的类型中编译器间preT算术EX pression。正如
One huge group of situations that critically relies on casts is arithmetic operations. The casting is required in situations when you need to force the compiler to interpret arithmetic expression within a type different from the "default" one. As in
unsigned i = ...;
unsigned long s = (unsigned long) i * i;
要避免溢出。或
double d = (double) i / 5;
为了使编译器切换到浮点除法。或
in order to make the compiler to switch to floating-point division. Or in
s = (unsigned) d * 3 + i;
,以便采取的浮点值的整个部分。等(例子是无穷无尽的)。
in order to take the whole part of the floating point value. And so on (the examples are endless).
有效利用的另一组是成语,即完善的编码实践。例如,经典的C成语当一个函数需要一个const指针作为输入,并返回一个非const指针一样的(潜在的常数)数据,如标准的strstr
例如。实现这个成语通常需要以抛弃输入的一个常量性用铸造的。有人可能会称之为糟糕的设计,但在现实中有C.没有更好的设计选择,否则它不会是一个行之有效的成语:)
Another group of valid uses is idioms, i.e. well-established coding practices. For example, the classic C idiom when a function takes a const pointer as an input and returns a non-const pointer to the same (potentially constant) data, like the standard strstr
for example. Implementing this idiom usually requires a use of a cast in order to cast away the constness of the input. Someone might call it bad design, but in reality there's no better design alternative in C. Otherwise, it wouldn't be a well-established idiom :)
另外值得一提的是,作为一个例子,一个迂腐正确使用标准的的printf的
功能可能需要在一般的情况下的参数转换。 (如%P
格式说明期待无效*
指针参数,这意味着一个为int *
参数已被改造成无效*
以这种或那种方式。有明确的转换是执行转换的最合理的方式)。
Also it is worth mentioning, as an example, that a pedantically correct use of standard printf
function might require casts on the arguments in general case. (Like %p
format specifier expecting a void *
pointer as an argument, which means that an int *
argument has to be transformed into a void *
in one way or another. An explicit cast is the most logical way to perform the transformation.).
当然,也有完全有效的情况下,其他无数的例子都需要类型转换时。
Of course, there are other numerous examples of perfectly valid situations when casts are required.
与注塑问题通常出现,当人们不假思索地使用它们,即使它们不是必需的(如铸造的malloc
,这是不好的不止一个原因返回)。或者,当人们使用强制类型转换强制编译器接受他们的坏code。不用说,它需要一定的专业知识水平,告诉从一个坏投一个有效的投局面。
The problems with casts usually arise when people use them thoughtlessly, even where they are not required (like casting the return of malloc
, which is bad for more reasons than one). Or when people use casts to force the compiler to accept their bad code. Needless to say, it takes certain level of expertise to tell a valid cast situation from a bad cast one.
在某些情况下,铸件被用来使编译器停止发放一些烦人的和不必要的警告消息。这些铸件属于好的和坏的演员之间的灰色地带。在一方面,管型不必要的都是不好的。在另一方面,用户可能没有在编译设置控制,从而使铸件处理警告的唯一途径。
In some cases casts are used to make the compiler to stop issuing some annoying and unnecessary warning messages. These casts belong to the gray area between the good and the bad casts. On the one hand, unnecessary casts are bad. On the other hand, the user might not have control over the compilation settings, thus making the casts the only way to deal with the warnings.
这篇关于需要关于C铸造一些澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!