0

我有这些结构:

typedef struct memory_slice {
    size_t startAdd; //Start of slice
    size_t dim; //Dim in bytes
    void *slice; //Chunk of memory
    struct memory_sclice *next; //Next element in list
} ms;

typedef struct heap_s {
    struct memory_slice *empty; //List of ptr to free memory
    struct memory_slice *used;
    heap_policy_t pol;
} heap_t;

我创建了两个作为构造函数和析构函数的函数:

heap_t *heapCreate (size_t heapSize) {
    heap_t *p = malloc(sizeof(heap_t));

    if (heapSize != 0) {
        p->empty = malloc(sizeof(ms)); //Only one chunk
        p->empty->slice = malloc (sizeof(heapSize));
        p->empty->dim = heapSize;
    } else {
        p->empty = malloc(sizeof(ms));
        size_t ap = 1024;
        p->empty->slice = malloc (sizeof(ap));
        p->empty->dim = ap;
    }

    p->empty->startAdd = 0;
    p->empty->next = NULL;
    p->used = NULL;
    p->pol = first_fit;

    return p;
}

//Destructor of struct
void heapDestroy (heap_t *h) {
    //Free all slices
    struct memory_sclice *app;

    while (h->empty != NULL) {
        free(h->empty->slice);
        app = h->empty;
        h->empty = h->empty->next;
        free(app);
    }

    while (h->used != NULL) {
        free(h->used->slice);
        app = h->used;
        h->used = h->used->next;
        //free(app->slice);
        free(app);
    }

    free(h); //Delete main structure
}

这段代码有效,但我不明白我不能像“free(app->slice)”这样释放内存:

while (h->empty != NULL) {
        app = h->empty;
        free(app->slice);
        h->empty = h->empty->next;
        free(app);
    }

有人能告诉我使用“free(app->slice)”的问题在哪里吗?

4

3 回答 3

1

提供的代码有几个参考struct memory_cslice;那些需要更改为struct memory_slice. 除非您添加以下内容,否则代码不会编译:

typedef enum { first_fit, best_fit, worst_fit } heap_policy_t;

你有和使用valgrind过吗?

有两个有问题的调用malloc()(在实际代码中不相邻):

p->empty->slice = malloc(sizeof(heapSize));
...
p->empty->slice = malloc(sizeof(ap));

在每种情况下, 的参数sizeof()都是 type size_t,因此实际上这两个块分配了相同数量的空间,并且可能是 4 字节或 8 字节。很多时候使用sizeof()with是正确的malloc();这是不正确的情况之一。如果你写了,你会简化你的代码:

heap_t *heapCreate(size_t heapSize)
{
    heap_t *p = malloc(sizeof(heap_t));

    if (heapSize == 0)
        heapSize = 1024;
    p->empty = malloc(sizeof(ms));
    p->empty->slice = malloc(heapSize);
    p->empty->dim = heapSize;
    p->empty->startAdd = 0;
    p->empty->next = NULL;
    p->used = NULL;
    p->pol = first_fit;
    return p;
}

当然,您应该处理malloc()失败的情况,因此应该改进为:

heap_t *heapCreate(size_t heapSize)
{
    heap_t *p = malloc(sizeof(heap_t));
    if (p != 0)
    {
        if (heapSize == 0)
            heapSize = 1024;
        if ((p->empty = malloc(sizeof(ms))) == 0)
        {
            free(p);
            return 0;
        }
        if ((p->empty->slice = malloc(heapSize)) == 0)
        {
            free(p->empty);
            free(p);
            return 0;
        }
        p->empty->dim = heapSize;
        p->empty->startAdd = 0;
        p->empty->next = NULL;
        p->used = NULL;
        p->pol = first_fit;
    }
    return p;
}
于 2013-05-12T03:36:40.750 回答
0

我相信这是因为你在这里有一个错字:

struct memory_sclice *app;

应该是memory_slice

目前,app不是类型memory_slice,因此slice不是成员。

基本上,您有一个指向没有定义的结构的指针。(除非出于某种非常奇怪的原因,您在那里有一个名为memory_sclice:V 的结构。我会感到惊讶。)

这个错误真的是唯一弹出的错误吗?

于 2013-05-11T21:52:26.317 回答
0

如果您想构建自己的玩具内存管理器,您应该使用 unix 函数 brk 和 sbrk :)

如果您向我们提供您在代码中调用 free(app->slice) 的具体上下文,以使其中断,那么回答肯定会容易得多。这是因为您应该能够通过执行 free(app->slice) 来释放内存。

于 2013-05-11T22:04:53.513 回答