在C中使用简单的链接列表实现,如何告诉Splint我转移了data
的所有权?
typedef struct {
void* data;
/*@null@*/ void* next;
} list;
static /*@null@*/ list* new_list(/*@notnull@*/ void* data)
{
list* l;
l = malloc(sizeof(list));
if (l == NULL)
return NULL;
l->next = NULL;
l->data = data;
return l;
}
我收到此错误消息:
Implicitly temp storage data assigned to implicitly
only: list->data = data
Temp storage (associated with a formal parameter) is transferred to a
non-temporary reference. The storage may be released or new aliases created.
(Use -temptrans to inhibit warning)
我想告诉Splint释放data
的责任转移到列表数据结构。
答案 0 :(得分:1)
解决方案位于function interfaces的Splint手册中。基本上,将函数签名更改为:
static /*@null@*/ list* new_list(/*@notnull@*/ /*@only@*/ void* data)
/*@defines result->data @*/
虽然我们在执行此操作时会收到新错误:
int main()
{
list* l = new_list("hej");
return 0;
}
Observer storage passed as only param:
new_list ("hej")
Observer storage is transferred to a non-observer reference. (Use
-observertrans to inhibit warning)