簡體   English   中英

釋放靜態字符串的動態數組

[英]freeing a dynamic array of static strings

所以我有一個問題,我有一個char * s數組(聲明為)char **,其中該數組是動態分配的(通過calloc),但是其中的char * s是靜態的。

在我嘗試釋放數組之前(在調整大小期間),這對我來說效果很好,此時我得到

*** glibc detected *** ./hash_table_test: free(): invalid next size (normal): 0x0891f028 ***

我嘗試將數組中的所有ptrs設置為NULL,但隨后出現錯誤

*** glibc detected *** ./hash_table_test: double free or corruption (!prev): 0x0815b028 ***

以下是相關代碼:

表結構:

struct string_hash_table {

//Array of c-strings
char** table;
//number of elements in the table
int num_elements;
//size of table
int table_size;

//Primes
int *primes;
//Current position in primes array
int primes_index;
//the size of the primes array
int primes_size;
};
//TypeDefs--------------------------------
typedef struct string_hash_table HashTable;

哈希函數(錯誤源)

void rehash_string(HashTable *table) {

int prev_size = table->table_size;
int i;

table->table_size = table->table_size * 2;

//create new array
char** new_table = calloc(table->table_size, sizeof(char*));
printf("new table created\n");
int index;
printf("before loop prev_size is %d\n", prev_size);
//add all elements to new_table
for (i = 0; i < prev_size; i++) {
    printf("on %d\n", i);
    index = find_spot_string(new_table, table->table_size, table->table[i]);
    printf("after find_spot_string\n");
    if (index != -1) {
        table->table[index] = table->table[i];
    }
}

//free and swap
printf("before free\n");
empty_string_array(table->table, table->table_size);
free(table->table);
table->table = new_table;

初始化HashTable結構:

//Takes a HashTable and initializes it
void init_hash_table(HashTable *table) {

table->primes_index = 0;
table->num_elements = 0;
table->primes_size = 297;
table->primes = prime_list;

table->table_size = table->primes[0];
table->table = calloc(table->table_size, sizeof(char*));
}

靜態字符串的聲明:

    char* temp = "hello";
add_hash_table_string(table, temp);

temp = "luck";
add_hash_table_string(table, temp);

temp = "stuck";
add_hash_table_string(table, temp);

temp = "buck";
add_hash_table_string(table, temp);

temp = "muck";
add_hash_table_string(table, temp);

temp = "much";
add_hash_table_string(table, temp);

目前,我只是在這里測試我的代碼,除了上面的重新哈希函數外,其他所有東西都可以正常工作。 有人有想法么? 還是我應該遵循的線索?

編輯:添加代碼為add_hash_table_string

void add_hash_table_string(HashTable *table, char* element) {

    //if non-null element, and element is not in the HashTable
    if (element != NULL && contains_hash_table_string(table, element) == 1) {

        //if the table is full
        if (table->table_size / 2 < table->num_elements) {

            rehash_string(table);
        }

        int index = find_spot_string(table->table, table->table_size, element);

        table->table[index] = element;
        table->num_elements++;
    }
}

編輯2:

忘了精確,錯誤發生在rehash函數中帶有free(table-> table)的行

一個可能的問題,您用新的大小釋放舊表

empty_string_array(table->table, table->table_size);

另一個可能是

index = find_spot_string(new_table, table->table_size, table->table[i]);
printf("after find_spot_string\n");
if (index != -1) {
    table->table[index] = table->table[i];

如果應該將條目復制到new_table ,則不會使用new_table index大於prev_size ,您將寫超出table末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM