-
21-09-2019 - |
题
我一直在尝试夹板与C程序我最近写了,并试图了解并删除警告它给。一个我理解,但不明白如何删除它来自下面的代码片段:
static MyType_t *findById(const int id)
{
int i;
for (i = 0; i < MY_ARR_SIZE; i++) {
if (my_arr[i].id == NOT_SET) {
/* Items are sorted so that items with
NOT_SET as ID are at the end of the array */
break;
}
if (my_arr[i].id == id) {
return &(my_arr[i]);
}
}
return NULL;
}
夹板不开心的是,函数可以返回NULL,但在这种情况下,它非常有意义。
我试着用/ @nullwhenfalse @ 的/但似乎如果函数返回真/假,也试图改变使用retVal的代码,并试图两/ 应用于才起作用@空@ 的/和/的 @relnull @ 的/在声明的前面,但这些也没做。
(正如一个侧面说明,该表仅仅是20个大气压,在使用一个聪明的搜索算法,以便没有任何意义。)
解决方案
您应该仔细检查使用/ * @零@ * /在声明的前面。
在您的示例的以下编译版本,但它确实删除警告(使用夹板3.1.2):
typedef struct { int id; } MyType_t;
#define NOT_SET -1
#define MY_ARR_SIZE 20
static MyType_t my_arr[MY_ARR_SIZE];
/*@null@*/ static MyType_t *findById(const int id)
{
int i;
for (i = 0; i < MY_ARR_SIZE; i++) {
if (my_arr[i].id == NOT_SET) {
/* Items are sorted so that items with
NOT_SET as ID are at the end of the array */
break;
}
if (my_arr[i].id == id) {
return &(my_arr[i]);
}
}
return NULL;
}
int main() {
(void)findById(10);
return 0;
}
如果您仍然有类似的警告,会不会是你的代码的其他部分?
其他提示
splint -nullret
将压扁的警告(全球),这可能是也可能不是你想要做什么。在某些情况下,除非你是的确定的有类型的返回NULL是正确的,你可能的要的警告。
我测试杰罗姆的例子,它没有住嘴该特定功能的警告。
不管你做什么,我会强烈建议不要直接嵌入夹板代码到源,而是在宏换行功能。
例如,用在鹦鹉项目,我有这些宏
# define ARGIN(x) /*@in@*/ /*@notnull@*/
# define ARGIN_NULLOK(x) /*@in@*/ /*@null@*/
/* The pointer target must be completely defined before being passed */
/* to the function. */
# define ARGOUT(x) /*@out@*/ /*@notnull@*/
# define ARGOUT_NULLOK(x) /*@out@*/ /*@null@*/
/* The pointer target will be defined by the function */
然后使用宏,以便我们可以使用:
void copy_string( ARGOUT(char *target), ARGIN(const char *source ) ) ...
如果我们想改变ARGIN()的参数是如何处理的,我们改变一个地方。我们也可以支持多个符号对多种工具或编译器。
不隶属于 StackOverflow