부목을 사용하는 데 문제가 발생했습니다. 여기에 기능 GetMem으로의 인수 밖으로 들어부목에 적용 할 인수에서 메모리 포인터를 반환하는 방법
#include <stdio.h>
#include <stdlib.h>
static void getMem(/*@[email protected]*/void **out, size_t size)
{
if(out == NULL)
return;
*out = malloc(size);
}
int main(/*@[email protected]*/int argc, /*@[email protected]*/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 /*@[email protected]*/ 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 /*@[email protected]*/ to denote passed or returned storage which need not be defined.
(Use -compdef to inhibit warning)
main.c:10:12: Storage **out allocated
, 내가 사용하기 전에 NULL 포인터를 확인해야합니다. 그런 다음 메모리 주소를 반환합니다. 어노테이션 "/ @ out @ /"은 함수에서 사용되었으므로 첫 번째 인수 앞에 넣을 수 없습니다. "/ @ null @ /"은 null이 될 수 있지만 out이 아님을 나타냅니다. 나는 그것을 어떻게 다룰 지 모른다. 아무도 조언을 줄 수 있습니까? 미리 감사드립니다.
getMem이 포인터를 할당하는 대신 포인터를 메모리로 리턴 할 수없는 이유가 있습니까? –
'정적 void getMem (/ * @ null @ */void * (/ * @ null @ */* out), size_t size)'가 작동하는지 궁금합니다 ... 지금 당장은 그것을 시도 할 수 없습니다. – Floris
@JTrana 회사의 코드 스타일이므로 변경하기가 어렵습니다. – Catro