gpt4 book ai didi

C:值被更改/覆盖

转载 作者:行者123 更新时间:2023-11-30 17:52:20 25 4
gpt4 key购买 nike

所以这个函数(tmap_insert)正在做一些奇怪的事情。我在循环中调用它,由于某种原因,每当我添加新项目时,它都会覆盖树中所有先前添加的项目的 item->name (但不是 item->val!),为其指定名称最近添加的。我已经在这里发布了我的代码供您查看——它很长,但我对此无能为力。

此外,我还包含了相关的结构以及在循环中调用它的函数。

int tmap_insert(TMAP_PTR t, char * name, double val){
TMAP_PTR parent = malloc(sizeof(struct tmap_struct));
TMAP_PTR new = malloc(sizeof(struct tmap_struct));
NAME_VAL* temp = malloc(sizeof(NAME_VAL));
temp->name = name;
temp->value = val;
new->item = temp;
new->size = 1;
new->height = 0;
while (t != NULL)
{
t->size = t->size + 1;
if (val > (t->item)->value)
{
parent = t;
t = t->right;
}
else if (val < (t->item)->value)
{
parent = t;
t = t->left;
}
}
if (parent != NULL)
{
new->parent = parent;
if (val > (parent->item)->value)
{
parent->right = new;
}
else if (val < (parent->item)->value)
{
parent->left = new;
}
}
TMAP_PTR unbalanced = malloc(sizeof(struct tmap_struct));
unbalanced = NULL;
TMAP_PTR iterator = malloc(sizeof(struct tmap_struct));
iterator = new->parent;
while (iterator != NULL)
{
if (iterator->left != NULL && iterator->right != NULL)
{
if ((iterator->left)->size > (2*((iterator->right)->size) + 1) || (iterator->right)->size > (2*((iterator->left)->size) + 1))
{
unbalanced = iterator;
}
if ((iterator->left)->height > (iterator->right)->height)
{
iterator->height = (iterator->left)->height + 1;
}
else
{
iterator->height = (iterator->right)->height + 1;
}
}
else if (iterator->left != NULL)
{
if ((iterator->left)->size > 1)
{
unbalanced = iterator;
}
iterator->height = (iterator->left)->height + 1;
}
else
{
if ((iterator->right)->size > 1)
{
unbalanced = iterator;
}
iterator->height = (iterator->right)->height + 1;
}
iterator = iterator->parent;
}
if (unbalanced != NULL)
{
NAME_VAL **arr = malloc(unbalanced->size * sizeof(NAME_VAL*));
int i;
for (i = 0; i < unbalanced->size; i++)
{
arr[i] = malloc(sizeof(NAME_VAL));
}
int *index = malloc(sizeof(int));
*index = 0;
arr = make_arr(unbalanced, arr, index);

int pos = ((unbalanced->size) / 2);

TMAP_PTR head = malloc(sizeof(struct tmap_struct));
head->size = 1;
head->height = 0;
head->item = arr[pos];
rebalance(arr, 0, pos-1, head);
rebalance(arr, pos+1, unbalanced->size, head);
unbalanced->parent = head->parent;
if ((unbalanced->parent)->right == unbalanced)
{
(unbalanced->parent)->right = head;
}
else
{
(unbalanced->parent)->left = head;
}
}
return 1;
}


TMAP_PTR tmap_create(char *fname){
printf("%s", fname);
fflush(stdout);
FILE *src;
src = fopen(fname, "r");
if (src == NULL)
{
printf("File could not open!\n");
return;
}
double value;
char name[20];

TMAP_PTR head = malloc(sizeof(struct tmap_struct));
head->size = 1;
head->height = 0;

fscanf(src, "%s", name);
fscanf(src, "%lf", &value);
head->item = malloc(sizeof(NAME_VAL));
(head->item)->value = value;
(head->item)->name = name;

while (fscanf(src, "%s %lf", name, &value) == 2)
{
printf("%s", name);
fflush(stdout);
tmap_insert(head, name, value);
}
return head;

}

struct tmap_struct{
TMAP_PTR parent;
TMAP_PTR left;
TMAP_PTR right;
int height;
int size;
NAME_VAL* item;
};

typedef struct name_val {
char *name;
double value;
}NAME_VAL;

最佳答案

在 tmap_create(char *fname) 中,您为 name 分配一个 char 数组,然后在循环的每个步骤中,您在其中读取 name 的新值,并将其传递给函数 tmap_insert ,您可以在其中创建一个新节点并将指向 char 数组的指针存储在其中。因此,显然,树的所有节点都指向同一个 char 数组,当您在其中存储新字符串时,所有节点都会看到新值。解决方案是为每个必须存储的名称分配一个新的字符数组。您应该将 tmap_create() 中的 while 循环替换为:

char* name = malloc(20*sizeof(char));
while (fscanf(src, "%s %lf", name, &value) == 2)
{
printf("%s", name);
fflush(stdout);
tmap_insert(head, name, value);
name = malloc(20*sizeof(char));
}

关于C:值被更改/覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16268520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com