简体   繁体   English

将参数传递给C语言中的函数时的隐式转换

[英]Implicit cast when passing arguments to functions in C language

suppose we have a function declared as the following: 假设我们有一个声明如下的函数:

void myFunct(unsigned char a);

In my main program i call it this way myFunct(5000) and i get this warning: "integer conversion resulted in truncation". 在我的主程序中,我称之为myFunct(5000)并且我收到此警告:“整数转换导致截断”。 The compiler warned me that i passed a value larger than 255. 编译器警告我,我传递的值大于255。

Anyway, if a declare a variable const ulong test = 5000 and i pass it to myFunct myFunct(test) , the compiler does not warn me about the same possible issue. 无论如何,如果声明一个变量const ulong test = 5000并且我将它传递给myFunct myFunct(test) ,编译器不会警告我同样可能的问题。

Can anyone explain me this behaviour? 谁能解释我这种行为?

This missing warning caused an annoying bug in my code and i am now afraid that these kind of issues could be present elsewhere. 这个丢失的警告在我的代码中引起了一个恼人的错误,我现在担心这些问题可能出现在其他地方。

I've tried different compilers like MinGW and GHS Version 5(GreenHills) and both did not warn me about the reported issue. 我尝试了不同的编译器,如MinGW和GHS第5版(GreenHills),两者都没有提醒我有关报告的问题。

Can anyone tell me if there is a way to prevent such problems? 谁能告诉我是否有办法防止这样的问题?

There does not need to be any logic to compiler warnings. 编译器警告不需要任何逻辑。 Compilers try to produce useful warnings about issues you may want to know about, and to avoid wasting your time with false positives. 编译器会尝试针对您可能想要了解的问题提供有用的警告,并避免因误报而浪费您的时间。 Any heuristic that satisfies this two conflicting goals may be implemented in a compiler. 任何满足这两个冲突目标的启发式都可以在编译器中实现。

The most likely explanation here is that in myFunct(5000) , it is obvious that the conversion fails to preserve the value, whereas in myFunct(test) , it is not obvious looking only at the function call. 这里最可能的解释是,在myFunct(5000) ,很明显转换无法保留该值,而在myFunct(test) ,仅查看函数调用并不明显。 Warning about the latter would require knowing the value of test at that point, and the compiler may not have the mechanisms to determine that the value of test at that point it 5000 . 关于后者的警告需要知道该点的test值,并且编译器可能没有机制来确定test值在该点为5000

Although it is obvious that the value of test is always 5000 in this particular example, the fact that a mechanism to predict the values of variables wouldn't work well in all cases (non-const variables the values of which are difficult to predict) may discourage compiler writers to even attempt to implement such a warning. 虽然在这个特定的例子中, test值显然总是5000 ,但是预测变量值的机制在所有情况下都不能很好地工作(非常量变量,其值很难预测)可能会阻止编译器编写者甚至尝试实现这样的警告。

Static analyzers settle for different goals than compilers. 静态分析仪的目标不同于编译器。 They try to predict values of variables in at least the easy cases, and some of them may be configurable to warn for myFunct(test) in your example. 他们尝试至少在容易的情况下预测变量值,其中一些可以配置为在您的示例中警告myFunct(test) Most static analyzers still reserve the right not to warn if they aren't certain that a problem is present, and you never know what they will be certain about or not. 大多数静态分析仪仍保留在不确定存在问题的情况下不发出警告的权利,并且您永远不知道他们将确定与否。 But you would have a better chance of getting a warning with a static analyzer than with a compiler. 但是使用静态分析器比使用编译器更有可能获得警告。

Note that what happens to the argument of myFunct in myFunct(5000) is a conversion . 需要注意的是会发生什么的争论myFunctmyFunct(5000)是一个转换 A cast is a syntactic construct. 演员阵容是一种句法结构。 There is no such thing as an “implicit cast”. 没有“隐性演员”这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM