C HeapValidate失败
我正试图建立一个这样的结构C HeapValidate失败,c,pointers,malloc,free,C,Pointers,Malloc,Free,我正试图建立一个这样的结构 typedef struct elements { struct elements *next; int key; struct value *val; }; typedef struct dict { struct dict_elements **dictionary_head; }; typedef struct value { int val; }; box->dictionary_head = (struct
typedef struct elements {
struct elements *next;
int key;
struct value *val;
};
typedef struct dict
{
struct dict_elements **dictionary_head;
};
typedef struct value
{
int val;
};
box->dictionary_head = (struct elements**)malloc(sizeof(struct elements*) * DICT_SIZE);
int DictInsert(struct dictionary_box *box, int key, struct value* value)
{
temp = (struct elements*) malloc(sizeof(struct elements*));
temp->key = key;
temp->val = value;
temp->next = NULL;
box->dictionary_head[1] = temp;
}
结构是这样创建的
typedef struct elements {
struct elements *next;
int key;
struct value *val;
};
typedef struct dict
{
struct dict_elements **dictionary_head;
};
typedef struct value
{
int val;
};
box->dictionary_head = (struct elements**)malloc(sizeof(struct elements*) * DICT_SIZE);
int DictInsert(struct dictionary_box *box, int key, struct value* value)
{
temp = (struct elements*) malloc(sizeof(struct elements*));
temp->key = key;
temp->val = value;
temp->next = NULL;
box->dictionary_head[1] = temp;
}
我插入一个元素,像这样
typedef struct elements {
struct elements *next;
int key;
struct value *val;
};
typedef struct dict
{
struct dict_elements **dictionary_head;
};
typedef struct value
{
int val;
};
box->dictionary_head = (struct elements**)malloc(sizeof(struct elements*) * DICT_SIZE);
int DictInsert(struct dictionary_box *box, int key, struct value* value)
{
temp = (struct elements*) malloc(sizeof(struct elements*));
temp->key = key;
temp->val = value;
temp->next = NULL;
box->dictionary_head[1] = temp;
}
但是当我这样去移除元素的时候
void DictRemove(struct dictionary_box *box, MHANDLE key)
{
struct elements *next = box->dictionary_head[1]->next;
free((box->dictionary_head[1])->val);
free(box->dictionary_head[1]); <<<-----------HeapValidate Assert failure
box->dictionary_head[1] = next;
....
}
插入元素的代码看起来可疑:
temp = (struct elements*) malloc(sizeof(struct elements*));
...fill the values...
我认为您想要的是sizeof(struct elements),而不是指针的大小:
temp = (struct elements*) malloc(sizeof(struct elements));
...fill the values...
插入元素的代码看起来可疑:
temp = (struct elements*) malloc(sizeof(struct elements*));
...fill the values...
我认为您想要的是sizeof(struct elements),而不是指针的大小:
temp = (struct elements*) malloc(sizeof(struct elements));
...fill the values...
插入元素的代码看起来可疑:
temp = (struct elements*) malloc(sizeof(struct elements*));
...fill the values...
我认为您想要的是sizeof(struct elements),而不是指针的大小:
temp = (struct elements*) malloc(sizeof(struct elements));
...fill the values...
插入元素的代码看起来可疑:
temp = (struct elements*) malloc(sizeof(struct elements*));
...fill the values...
我认为您想要的是sizeof(struct elements),而不是指针的大小:
temp = (struct elements*) malloc(sizeof(struct elements));
...fill the values...
请试着想出一个解决办法。您选择性地给了我们一些代码(甚至不是真正的代码,因为
value
在结构中不是有效字段)。问题甚至可能不在您认为的地方。temp=(struct elements*)malloc(sizeof(struct elements*))代码>未分配正确的内存量。它需要sizeof(struct elements)
。在您的程序中是否在其他地方定义了struct dict\u elements
,或者您实际上是指struct dict.dictionary\u head
具有类型struct elements**
?请尝试提出一个解决方案。您选择性地给了我们一些代码(甚至不是真正的代码,因为value
在结构中不是有效字段)。问题甚至可能不在您认为的地方。temp=(struct elements*)malloc(sizeof(struct elements*))代码>未分配正确的内存量。它需要sizeof(struct elements)
。在您的程序中是否在其他地方定义了struct dict\u elements
,或者您实际上是指struct dict.dictionary\u head
具有类型struct elements**
?请尝试提出一个解决方案。您选择性地给了我们一些代码(甚至不是真正的代码,因为value
在结构中不是有效字段)。问题甚至可能不在您认为的地方。temp=(struct elements*)malloc(sizeof(struct elements*))代码>未分配正确的内存量。它需要sizeof(struct elements)
。在您的程序中是否在其他地方定义了struct dict\u elements
,或者您实际上是指struct dict.dictionary\u head
具有类型struct elements**
?请尝试提出一个解决方案。您选择性地给了我们一些代码(甚至不是真正的代码,因为value
在结构中不是有效字段)。问题甚至可能不在您认为的地方。temp=(struct elements*)malloc(sizeof(struct elements*))代码>未分配正确的内存量。它需要sizeof(struct elements)
。您的程序中是否在其他地方定义了struct dict_elements
,或者您实际上是指struct dict.dictionary_头
具有类型struct elements**
?或者更好的是temp=malloc(sizeof(*temp))代码>好的,现在似乎可以了,这只是一个尺寸问题…我不敢相信我为这样的问题浪费了3个小时,谢谢,或者更好,temp=malloc(sizeof(*temp))代码>好的,现在似乎可以了,这只是一个尺寸问题…我不敢相信我为这样的问题浪费了3个小时,谢谢,或者更好,temp=malloc(sizeof(*temp))代码>好的,现在似乎可以了,这只是一个尺寸问题…我不敢相信我为这样的问题浪费了3个小时,谢谢,或者更好,temp=malloc(sizeof(*temp))代码>好的,现在似乎可以了,只是尺寸问题…我不敢相信我因为这样的问题浪费了3个小时,谢谢