C指针,免费记忆

时间:2013-05-11 21:44:47

标签: c pointers malloc free

我有这些结构:

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
}

此代码有效,但我不明白我无法释放像这样的“免费(app->切片)”的记忆:

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

有人可以告诉我使用“免费(app->切片)”的问题在哪里吗?

3 个答案:

答案 0 :(得分: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()的参数都是size_t类型,因此实际上这两个块分配的空间量相同,可能是4个字节或8个字节。通常将sizeof()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;
}

答案 1 :(得分:0)

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

struct memory_sclice *app;

应为memory_slice

目前,app不属于memory_slice类型,因此slice不是会员。

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

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

答案 2 :(得分:0)

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

如果您在代码中为我们提供了您在其中调用free(app-> slice)的特定上下文,那么肯定会更容易回答,以便它能够中断。这是因为你应该通过免费(app->切片)来释放内存。