我如何解决散列时发生的malloc错误

问题描述 投票:1回答:1

在大量调试陷入此malloc错误之后,尝试使用哈希表实现字典

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


/* to store a data (consisting of key and value) in hash table array */
struct item 
{
    char key[100];
    char value[1000];
};

/* each hash table item has a flag (status) and data (consisting of key and value) */
struct hashtable_item 
{

    int flag;
    /*
     * flag = 0 : data does not exist
     * flag = 1 : data exists at given array location
     * flag = 2 : data was present at least once
    */
    struct item *data;

};

struct hashtable_item *array;
int size = 10007;
int max = 10007;

/ *此函数返回给定键的相应索引* /

int hashcode(char* key,int TS)
{
    return (key[0]+27*key[1]+729*key[2])%TS;
}

/* this  function initializes the hash table array */
void init_array()
{
    int i;
    for (i = 0; i < max; i++) 
        {
        array[i].flag = 0;
        array[i].data = NULL;
    }
}

/ *此函数在哈希表中插入一个元素* /

void insert(char* key, char* value,int TS)
{
    int index = hashcode(key,TS);

    int i = index;
    int h = 1;

    struct item *new_item = (struct item*) malloc(sizeof(struct item));
    strcpy(new_item->key , key);

    strcpy(new_item->value , value);


    /* probing through the array until an empty space is found */
    while (array[i].flag == 1) 
        {

        if (array[i].data->key == key) 
                {

            /* case when already present key matches the given key */
            printf("\n This key is already present in hash table, hence updating it's value \n");
            strcpy(array[i].data->value , value);
            return;

        }
        i = (i + (h * h)) % max;
        h++;
        if (i == index)
                {
            printf("\n Hash table is full, cannot add more elements \n");
            return;
        }

    }

    array[i].flag = 1;
    array[i].data = new_item;
    printf("\n Key (%s) has been inserted\n", key);
    size++;

}
  /* to remove an element form the hash table array */


void remove_element(char* key,int TS)
{
    int index = hashcode(key,TS);
    int i = index;
    int h = 1;

    /* probing through the hash table until we reach at location where there had not been an element even once */
    while (array[i].flag != 0)
        {
        if (array[i].flag == 1  &&  array[i].data->key == key) 
                {

            /* case where data exists at the location and its key matches to the given key */
            array[i].flag = 2;
            array[i].data = NULL;
            size--;
            printf("\n Key (%s) has been removed \n", key);
            return;

        }
        i = (i + (h * h)) % max;
        h++;
        if (i == index) 
                {
            break;
        }
    }
    printf("\n Key does not exist \n");

}
 /* to display the contents of hash table */


void display()
{

    int i;
    for(i = 0; i < max; i++)
        {
        if (array[i].flag != 1)
                {
            printf("\n Array[%d] has no elements \n", i);
        }
        else
                {
            printf("\n Array[%d] has elements \n  %s (key) and %s (value) \n", i, array[i].data->key, array[i].data->value);
        }
    }

}

int size_of_hashtable()
{
    return size;
}

主要功能

void main()
{
    int choice,n, c;
    char key[100];
    char value[1000];
    int TS=10007;
    array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item*));
    init_array();

    do {
        printf("Implementation of Hash Table in C with Quadratic Probing.\n\n");
        printf("MENU-: \n1.Inserting item in the Hash table" 
                              "\n2.Removing item from the Hash table" 
                              "\n3.Check the size of Hash table" 
                              "\n4.Display Hash table"
               "\n\n Please enter your choice-:");

        scanf("%d", &choice);

        switch(choice)
                {

        case 1:

              printf("Inserting element in Hash table \n");
              printf("Enter key:");
              fgets(key,100, stdin);
              printf("Enter value:");
              fgets(value,1000, stdin);
              insert(key, value,TS);

              break;

        case 2:

              printf("Deleting in Hash table \n Enter the key to delete-:");
              fgets(key,100, stdin);
              remove_element(key,TS);

              break;

        case 3:

              n = size_of_hashtable();
              printf("Size of Hash table is-:%d\n", n);

              break;

        case 4:

              display();

              break;

        default:

               printf("Wrong Input\n");

        }

        printf("\n Do you want to continue-:(press 1 for yes)\t");
        scanf("%d", &c);

    }while(c == 1);


}

使用二次探测在C中实现哈希表。这是输出的样子

MENU-: 
1.Inserting item in the Hash table
2.Removing item from the Hash table
3. Check the size of the Hash table
4.Display Hash table

 Please enter your choice-:1
Inserting element in Hash table 
Enter key:Enter value:john helllo we aer
a.out: malloc.c:2374: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end `enter code here`& pagemask) == 0)' failed.
Aborted (core dumped)
c hash malloc hashtable hashcode
1个回答
0
投票

通常sysmalloc错误会在破坏某些内存(例如从未分配的空间进行写入或读取等)时引发

我注意到的第一件事是数组的分配不是很正确。我发现您需要一个struct hashtable_item -s数组,但是您正在为指针数组分配空间。sizeof(struct hashtable_item*)在x64系统中将始终为8,因为它是指针的大小。您可能应该使用sizeof(struct hashtable_item),它将为您提供实际struct对象的大小。

您需要的是这样的东西:

array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item));

但是另一方面,由于您的array是静态的,建议您在stack中分配它,而不是heap

struct hashtable_item[10007];

此外,如果您可以提供有关insert()不在哪一行的信息,我们将不胜感激。

© www.soinside.com 2019 - 2024. All rights reserved.