【问题标题】:Invalid read of size 8 at .. by在 .. 处读取的大小为 8 的无效读取
【发布时间】:2019-10-15 03:46:29
【问题描述】:

它显示我的主函数在 HTSize 处读取的大小为 8 无效,并且它似乎对空哈希有效(它打印 0)但我认为问题是我的 HTCreate 中的初始化哈希作为参数传递在 HTSize 但未初始化。

我尝试过(不在显示的代码中)将参数作为 &Table 传递,定义为 HTSize(HThash**) 但没有解决它,程序没有运行并且 valgrind 显示“地址 0x0 不是堆栈'd, malloc'd 或(最近)free'd”。在函数内部,我还放置了一个指向哈希的指针 T 以取消引用 **hash。我在 linux 中工作。

typedef char* KeyType;
typedef int HTItem;
typedef struct node{
    KeyType key;
    HTItem item;
    struct node *next;
}List;
typedef struct {
    List *head;
}TableEntry;
typedef TableEntry *HTHash;

HTHash* HTCreate(void);
int HTSize(HTHash);

int TABLESIZE = 10;

int main(void)
{
    HTItem *p;
    HTHash *Table = HTCreate();
    printf("%d\n",HTSize(*Table));
}

HTHash* HTCreate(void)
{
    int i;
    HTHash table = (HTHash)malloc(TABLESIZE*sizeof(TableEntry));
    HTHash *T;
    for(i=0;i<TABLESIZE;i++)
        table[i].head = NULL;
    T = &table;
    return T;
}

int HTSize(HTHash hash)
{
    int i,count=0;
    List *temp = (List*)malloc(sizeof(List));   
    for(i=0;i<TABLESIZE;i++)
    {
        if(hash[i].head != NULL)
        {
            count++;
            temp = hash[i].head->next;
            while(temp != NULL)
            {
                count++;
                temp = temp->next;
            }
        }   
    }
    return count;   
}

错误可能在 HTCreate 中,但我不确定。我检查了哈希是否在主函数中初始化并且确实如此。

【问题讨论】:

标签: c hash segmentation-fault


【解决方案1】:

您的问题实际上出在HTCreate 函数中:它返回函数本地的&amp;table

演示:

HTHash *HTCreate(void)
{
    int i;

    HTHash table = NULL;

    printf("&table before malloc: %p\n", &table);

    table = malloc(TABLESIZE*sizeof(TableEntry));

    printf("&table after  malloc: %p\n", &table);

    HTHash *T;

    for(i=0;i<TABLESIZE;i++)
        table[i].head = NULL;

    T = &table;
    return T;
}

将打印:

&table before malloc: 0x7fff200eb818
&table after  malloc: 0x7fff200eb818

我们看到malloc 对这个值没有影响。

由于 this 对函数来说是本地的,所以在另一个函数中使用 this 可能会导致任何事情(未定义的行为)

从函数中返回所需内容的好方法是返回 HTHash 类型:

HTHash HTCreate(void)
{
    int i;

    HTHash table = malloc(TABLESIZE*sizeof(TableEntry));

    for(i=0;i<TABLESIZE;i++)
        table[i].head = NULL;

    return table;
}

看里面做了什么,发现这个功能可以简化:

HTHash HTCreate(void)
{
    return calloc(TABLESIZE, sizeof(TableEntry));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 2023-03-16
    • 2015-07-06
    • 2013-03-12
    相关资源
    最近更新 更多