我遇到了使用夹板的问题。这是类似的代码
#include <stdio.h>
#include <stdlib.h>
static void getMem(/*@null@*/void **out, size_t size)
{
if(out == NULL)
return;
*out = malloc(size);
}
int main(/*@unused@*/int argc, /*@unused@*/char *argv[])
{
char *str = NULL;
getMem((void **)&str, 1);
if(str != NULL)
{
*str = 'a';
(void)putchar(*str);
free(str);
}
return 0;
}
夹板给出这样的警告信息,
main.c: (in function getMem)
main.c:11:2: Function returns with possibly null storage derivable from
parameter *out
A possibly null pointer is reachable from a parameter or global variable that
is not declared using a /*@null@*/ annotation. (Use -nullstate to inhibit
warning)
main.c:10:12: Storage *out may become null
main.c:11:2: Function returns storage out reachable from parameter not
completely defined (**out is undefined)
Storage derivable from a parameter, return value or global is not defined.
Use /*@out@*/ to denote passed or returned storage which need not be defined.
(Use -compdef to inhibit warning)
main.c:10:12: Storage **out allocated
对于函数 getMem 中的参数,我需要在使用前检查 NULL 指针。然后返回内存地址。注解“/ @out@ /”不能放在第一个参数之前,因为它在函数中使用。而“/ @null@ /”只表示out可以为null,不能为*out。我不知道如何处理它。有人可以给一些建议吗?提前致谢。