在侵入式数据结构示例中堆积损坏

时间:2017-06-12 08:46:25

标签: c data-structures heap

这是一个双向链表,节点/链接包含所需信息以便存储在列表中(侵入性):

dlist.h

#ifndef DLIST_H
#define DLIST_H

//--------------------------------------------------------------------------

typedef struct Link
{
    struct Link* succ;
    struct Link* prev;
} Link;

//--------------------------------------------------------------------------

typedef struct List
{
    Link* first;
    Link* last;
} List;

//--------------------------------------------------------------------------

void init(List* lst)
{
    assert(lst);

    lst->first = 0;
    lst->last = 0;
}

//--------------------------------------------------------------------------

List* create()
{
    List* lst = (List*) malloc(sizeof(List*));

    init(lst);

    return lst;
}

//--------------------------------------------------------------------------

void push_back(List* lst, Link* l)
{
    assert(l);
    assert(lst);
    {
        Link* last = lst->last;

        if (last)
        {
            last->succ = l;
            l->prev = last;
        }
        else
        {
            lst->first = l;
            l->prev = 0;
        }

        lst->last = l;
        l->succ = 0;
    }
}

//--------------------------------------------------------------------------

void clear (List* lst)
{
    assert(lst);
    {
        Link* curr = lst->first;
        Link* next = 0;

        while (curr)
        {
            next = curr->succ;

            free(curr);

            curr = next;
        }

        lst->first = 0;
        lst->last = 0;
    }
}

//--------------------------------------------------------------------------

void destroy (List* lst)
{
    assert(lst);

    clear(lst);

    free(lst);
}

//--------------------------------------------------------------------------

typedef struct Name
{
    Link l;
    char* s;
} Name;

//--------------------------------------------------------------------------

Name* make_name(char* str)
{
    Name* n = (Name*) malloc(sizeof(Name*));
    n->s = str;

    return n;
}

//--------------------------------------------------------------------------

#endif

main.c

#include <stdlib.h>     // malloc
#include <stdio.h>      // printf
#include <assert.h>     // assert


#ifdef __cplusplus  


#else  // compiling in C. 

int main ()
{
    List* lst = create();

    char* names[ ] = { "Giorikas", "Kostikas", "Foo", "Bar", "Gosho", "Pesho" };
    char* name;

    int i = 0;
    int size = 6;

    for (i; i < size; ++i)
    {
        push_back(lst, (Link*)(make_name(names[i])));

        name = ((Name*)(lst->last))->s;
        printf("Name: %s \n", name);
    }

    destroy(lst);

    getchar();

    return 0;
}

#endif

使用调试器踩到我打印的名称并在函数clear()中,在第一次释放链接时,我得到两个警告,最后:

  

_CrtIsValidHeapPointer(pUserData)

注意:经过一些研究后我明白了:“在重写时你不会立即收到堆损坏,但在下一次堆检查 ,这将在任何堆上执行下一次内存分配/释放。“。因此,clear()中发生的事情可能是触发错误的堆检查。

这个堆腐败发生在哪里?

1 个答案:

答案 0 :(得分:5)

你做错了分配。您需要分配对象而不是指针,所以:

List* lst = (List*) malloc(sizeof(List*));

应该是

List* lst = (List*) malloc(sizeof(List));

至少Name也一样。你也可以删除演员:

List* lst = malloc(sizeof(List));

----- ---- EDIT

更好的习语是:

List* lst = malloc(sizeof(*lst));