【问题标题】:calloc not allocating correct data when returned to pointer reference from a dereferenced double pointer in a function从函数中取消引用的双指针返回到指针引用时,calloc 未分配正确的数据
【发布时间】:2021-10-24 05:16:37
【问题描述】:

我正在尝试使用 calloc 将数据动态分配给另一个结构中的结构指针。

如果我直接从主程序分配,没有问题。 如果我将双指针传递给以 ** 作为参数的初始化函数,并使用箭头运算符来引用地址,则数据未正确分配,并且在编码散列时出现“浮点错误”。

这段代码按照我想要的方式运行。

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


/*create item structure, this will hold allocated pointers to the key/value elements.*/
struct H_item_t{
    char* key;
    int* value;
};
typedef struct H_item_t H_item_t;

/*create the table structure, this will point to the initial item address.*/
struct HashTable_T {
    size_t size;
    H_item_t* items;
};
typedef struct HashTable_T HashTable_T;

int encodeKey(HashTable_T* table, char* key){
    /*encode the string*/
    size_t index = 0;   //initialize value to full
    size_t length = strlen(key);    //get length of string
    size_t i = 0; //initialize loop;.

    for (; i < length; i++) {
    index ^= (key[i]);
    };
    index %= table->size;
    return index;
}

void hashItem(HashTable_T* table, char* string, int value ){
    H_item_t* ptr1;
    int index = encodeKey(table,string);
    ptr1= &table->items[index];
    ptr1->key=string;
    ptr1->value=(int)value;
}

int* getValue(HashTable_T* table, char* key){
    int index = encodeKey(table,key);
    H_item_t* ptr1;
    ptr1= &table->items[index]; 
    return ptr1->value;
}

void deleteTable(HashTable_T* table){
    free(table->items);
    free(table);
};

void initTable(HashTable_T** table, int size){

    (*table) = (HashTable_T*)malloc(sizeof(HashTable_T));
    (*table)->items = (H_item_t*)calloc(size,sizeof(H_item_t));   
};

int main()
{

HashTable_T* ht1 = malloc(sizeof(HashTable_T));
ht1->size=10;
ht1->items=(H_item_t*)calloc(ht1->size,sizeof(H_item_t));
   
hashItem(ht1,"A",3);
hashItem(ht1,"B",2);
hashItem(ht1,"C",1);
hashItem(ht1,"D",100);

printf("value returned from get: %x\n",(int)getValue(ht1,"D"));

deleteTable(ht1);  
}

如果我使用 initTable() fxn;

HashTable_T* ht1;
initTable(&ht1,10);

代替;

HashTable_T* ht1 = malloc(sizeof(HashTable_T));
ht1->size=10;
ht1->items=(H_item_t*)calloc(ht1->size,sizeof(H_item_t));

运行时出现“浮点异常”

【问题讨论】:

  • initTable 没有设置size 字段。
  • 确保类型转换正确完成。 ptr1->value=(int*)value;
  • 第二个参数应该将此值设置为 10,然后将其作为 int 传递给 calloc。我也尝试过; initTable(&ht1,(int)10);
  • "第二个参数应该将此值设置为 10"。是的,它应该。但关键是所示代码并没有这样做。缺少(*table)-&gt;size = size;
  • @kaylum 谢谢!这就是问题所在。我会用解决方案回答它

标签: c gcc calloc double-pointer


【解决方案1】:

解决方案;

不要忘记初始化结构中的所有成员。

这是 initTable fxn 的工作代码;

void initTable(HashTable_T** table, int size){
    //return allocated memory to the pointer that is held in the double pointer
    (*table) = (HashTable_T*)malloc(sizeof(HashTable_T));
    (*table)->size = size;
    (*table)->items = (H_item_t*)calloc((*table)->size,sizeof(H_item_t));

};

我忘记在函数内部初始化结构内的 ..size。导致错误

【讨论】:

    猜你喜欢
    • 2019-03-11
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2013-07-01
    相关资源
    最近更新 更多