我有一个实现B + Tree的代码。订单是3
问题是如果我在树中插入第三个值(叶子被拆分)它会在行中抛出分段错误
temp_pointers = (void**) malloc(order * sizeof(void *));
以下是leaf split
的完整代码node * split_leaf_and_insert(node * root, node * leaf, char* key,
record * pointer) {
node * new_leaf = NULL;
char ** temp_keys = NULL;
void ** temp_pointers = NULL;
int insertion_index, split, i, j;
char*new_key;
new_leaf = create_leaf();
temp_keys = (char**) malloc(order * sizeof(char*));
if (temp_keys == NULL) {
exit(EXIT_FAILURE);
}
temp_pointers = (void**) malloc(order * sizeof(void *));
if (temp_pointers == NULL) {
exit(EXIT_FAILURE);
}
insertion_index = 0;
while (insertion_index < order - 1 && leaf->keys[insertion_index] < key) {
insertion_index++;
}
for (i = 0, j = 0; i < leaf->number_of_keys; i++, j++) {
if (j == insertion_index) {
j++;
}
temp_keys[j] = (char*) malloc(strlen(leaf->keys[i]) + 1);
strcpy(temp_keys[j], leaf->keys[i]);
}
temp_keys[insertion_index] = (char*) malloc(strlen(key) + 1);
strcpy(temp_keys[insertion_index], key);
temp_pointers[insertion_index] = pointer;
leaf->number_of_keys = 0;
split = cut(order - 1);
for (i = 0; i < split; i++) {
leaf->pointers[i] = temp_pointers[i];
leaf->keys[i] = (char*) malloc((strlen(temp_keys[i]) + 1));
strcpy(leaf->keys[i], temp_keys[i]);
leaf->number_of_keys++;
for (i = split, j = 0; i < order; i++, j++) {
new_leaf->pointers[j] = temp_pointers[i];
new_leaf->keys[j] = (char*) malloc(strlen(temp_keys[i]) + 1);
strcpy(new_leaf->keys[j], temp_keys[i]);
new_leaf->number_of_keys++;
}
free(temp_pointers);
free(temp_keys);
new_leaf->pointers[order - 1] = leaf->pointers[order - 1];
leaf->pointers[order - 1] = new_leaf;
for (i = leaf->number_of_keys; i < order - 1; i++) {
leaf->pointers[i] = NULL;
}
for (i = new_leaf->number_of_keys; i < order - 1; i++) {
new_leaf->pointers[i] = NULL;
}
new_leaf->parent = leaf->parent;
new_key = (char*) malloc(strlen(new_leaf->keys[0]) + 1);
strcpy(new_key, new_leaf->keys[0]);
}
return insert_value_into_parent(root, leaf, new_key, new_leaf);
}
就像我说的前两个记录一样,但是第三个记录是
Use of uninitialised value of size 8 when use malloc in c
The stack trace
==1336== Use of uninitialised value of size 8
==1336== at 0x4041F5: display (b-tree.c:754)
==1336== by 0x40519C: main (main.c:54)
==1336== Uninitialised value was created by a heap allocation
==1336== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1336== by 0x4036D0: split_leaf_and_insert (b-tree.c:597)
==1336== by 0x402E3C: insert_value (b-tree.c:477)
==1336== by 0x4029B1: search (b-tree.c:396)
==1336== by 0x404159: display (b-tree.c:739)
==1336== by 0x40519C: main (main.c:54)
==1336==
==1336== Invalid read of size 8
==1336== at 0x4041F5: display (b-tree.c:754)
==1336== by 0x40519C: main (main.c:54)
==1336== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==1336==
==1336==
下一个函数是调用上面的函数的插入函数
node * insert_value(node * root, char* key, record * rec) {
node * leaf;
if (root == NULL) {
return create_new_tree(key, rec);
}
leaf = find_leaf(root, key);
if (leaf->number_of_keys < order - 1) {
leaf = insert_value_into_leaf(leaf, key, rec);
return root;
}
return split_leaf_and_insert(root, leaf, key, rec);
}
有谁能解释一下发生了什么?