sách gpt4 ăn đã đi

条件跳转或移动取决于未初始化的值、结构

In lại Tác giả: Walker 123 更新时间:2023-11-30 19:35:35 26 4
mua khóa gpt4 giày nike

我有一个指向结构的指针数组,我正在尝试使用它们进行 while 循环。我对如何准确初始化它并不完全有信心,但我一直这样做:

Entry *newEntry = malloc(sizeof(Entry));

我有另一个函数,它是测试代码,如果数组中的点具有填充值或 null

Entry* current = hashTable->table[val];

while(current != NULL){

我只是使用 Entry *current 基本上作为临时变量来引用数组中存储的任何内容。这样做时,我收到 valgrind 错误: 条件跳转或移动取决于未初始化的值

我必须以某种方式初始化它吗?我想做的就是存储对数组中该点的引用..它可能为空,也可能不为空,如果它不为空,则执行 while 循环

这是我应该初始化的方式吗?

编辑-我有每个 Entry 结构的指针数组,我使用此代码对其进行初始化。我希望这是正确的方法。

Entry** arrayOfPointers;
arrayOfPointers = malloc(arraySize* sizeof(Entry*));

编辑2-抱歉,如果上面的内容并不像我想象的那么简单。这是一个最小的例子

//create arry of pointers
Entry** arrayOfPointers;
arrayOfPointers = malloc(arraySize* sizeof(Entry*));

//create elements at certain indexs in array
Entry *newEntry = malloc(sizeof(Entry));
arrayOfPointers[1] = newEntry;
//this is the gist of what this does

//Search and do something when found something in array
//since the array is pretty big, theres going to be indexes when theres nothing
for(int i = 0; i < arraySize; i++){
//temp holder
Entry *temp = arrayOfPointers[i];
if(temp != NULL){
//Do Stuff
//This if statement is where the conditional jump or move error will be thrown
//not exactly sure what but somehow do not initialize temp correctly
}
}

câu trả lời hay nhất

malloc 可能会失败。因此您需要对其进行测试。

Entry *newEntry = malloc(sizeof(Entry));
if (newEntry != NULL)
{
/* Use it */
free(newEntry);
}
khác
{
printf("error - malloc failed\n");
}

该代码为一个条目分配空间。我怀疑你打算将它作为一个数组

int elementCount = 1000;
Entry *newEntry = malloc(sizeof(Entry) * elementCount);

获得动态分配的Entry数组后,您需要初始化数组中的值

for (int z=0; z < elementCount; z++)
{
newEntry[z] = {whatever value is needed to do the initialization};
}

然后您可以开始散列并返回散列元素的地址,并用它做一些事情。

关于条件跳转或移动取决于未初始化的值、结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42469624/

26 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress