【问题标题】:Transfer ownership of storage in Splint转移 Splint 中的存储所有权
【发布时间】:2014-10-10 15:50:01
【问题描述】:

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

【问题讨论】:

    标签: c ownership splint


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多