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

Zie*_*ezi 1 c heap data-structures

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

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

踩着调试器我得到了打印和功能的名称clear(),在第一次释放链接时,我得到两个警告,最后:

_CrtIsValidHeapPointer(pUserData)

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

这个堆腐败发生在哪里?

Jea*_*nès 5

你做错了分配.您需要分配不是指针的对象,因此:

List* lst = (List*) malloc(sizeof(List*));
Run Code Online (Sandbox Code Playgroud)

应该

List* lst = (List*) malloc(sizeof(List));
Run Code Online (Sandbox Code Playgroud)

并且Name至少相同.你也可以删除演员表:

List* lst = malloc(sizeof(List));
Run Code Online (Sandbox Code Playgroud)

- - -编辑 - -

而且更好的习语是:

List* lst = malloc(sizeof(*lst));
Run Code Online (Sandbox Code Playgroud)