1

使用 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已转移到列表数据结构中。

4

1 回答 1

1

解决方案在功能接口的 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)
于 2014-08-17T17:22:54.910 回答