gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:26:19 25 4
gpt4 key购买 nike

当我对 C 程序进行 valgrind 时,出现以下错误:

Conditional jump or move depends on uninitialised value(s)  
==7092== at 0x40298E: main (search.c:214)
==7092== Uninitialised value was created by a heap allocation
==7092== at 0x4C274A8: malloc (vg_replace_malloc.c:236)
==7092== by 0x40211B: createEntry (words.c:113)
==7092== by 0x4027D9: parseIndex (search.c:170)
==7092== by 0x402903: main (search.c:208)

通常这表明我没有为结构分配正确的大小,但我认为情况并非如此。这是所有相关代码,请告诉我您的想法!

/* Entry_
*
* @param filename filename and path
* @param frequency how often the word appears
* @param next next entry in list
*/

struct Entry_ {
char *filename;
int frequency;
struct Entry_* next;
};

typedef struct Entry_* Entry;

这是创建条目函数的代码:

/* createEntry
*
* Creates a brand new entry object.
*
* @param filename the filename where the entry occured
*
* @return success new Entry
* @return failure NULL
*/

Entry createEntry(char *filename)
{
Entry ent;

ent = (Entry) malloc( sizeof(struct Entry_) ); /* This is line 113 in words.c */
if(ent == NULL)
{
fprintf(stderr, "Error: Could not allocate memory for Entry.\n");
return NULL;
}

ent->filename = (char*) malloc( sizeof(char) * (strlen(filename) + 1));
if(ent->filename == NULL)
{
free(ent);
fprintf(stderr, "Error: Could not allocate memory for Entry.\n");
return NULL;
}

strcpy(ent->filename, filename);

ent->frequency = 1;

return ent;
}

编辑:在 Search.c 中添加了代码

/* parseIndex
*
* Function that takes in an inverted index and returns
* a HashTable containing all the words and their entries.
* Returns NULL on failure.
*
* @param filename name of the inverted index
*
* @return success new HashTable
* @return failure NULL
*/

HashTable parseIndex(char* filename)
{
HashTable table;
TokenizerT tok;
char* str;
int res;
Entry ent;

if(filename == NULL)
{
fprintf(stderr, "Error: Cannot parse NULL file.\n");
return NULL;
}

table = createHT(hash, compStrings, destroyString, destroyWord, printWordHT);
if(table == NULL)
{
fprintf(stderr, "Error: Could not allocate space for HashTable.\n");
return NULL;
}

tok = TKCreate(FILE_CHARS, filename);
if(tok == NULL)
{
fprintf(stderr, "Error: Could not allocate space for Tokenizer.\n");
return NULL;
}

str = TKGetNextToken(tok);
res = strcmp(str, "files");
free(str);

if(res != 0)
{
fprintf(stderr, "Error: Malformed index file.\n");
return NULL;
}

/* Parse the file list */
while((str = TKGetNextToken(tok)) != 0 && strcmp(str, "/files") != 0)
{
free(str);
str = TKGetNextToken(tok);

if(str == 0 || strcmp(str, "/files") == 0)
{
fprintf(stderr, "Error: Malformed index file.\n");
return NULL;
}

ent = createEntry(str); /* Line 170 */

if(file_list == NULL)
{
file_list = ent;
}
else
{
ent->next = file_list;
file_list = ent;
}

free(str);
}

free(str);

TKDestroy(tok);
tok = NULL;

return table;
}

int main( int argc, char** argv )
{
HashTable table;
Entry curr, next;
int i;

/* Validate the inputs */
if( (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h') || argc != 2 )
{
fprintf(stderr, "Usage: %s <inverted-index filename>\n", argv[0]);
return 1;
}

file_list = NULL;

table = parseIndex(argv[1]); /* Line 208 */
assert(table != NULL);

curr = file_list;
i = 0;

while(curr != NULL)
{
next = curr->next;

printf("[%i]: %s\n", i, curr->filename);

free(curr->filename);
free(curr);

curr = next;
}

destroyHT(table);
table = NULL;

return 1;
}

最佳答案

您的createEntry函数不初始化 next field 。统一使用某些内容的实际点位于您所显示的代码之外(在 search.c 的第 214 行),但从您所显示的内容来看,我的猜测是 next字段。

其他一些挑剔:

  • 将指针隐藏在 typedef 中通常会令人困惑。这使得人们不清楚读者是否忘记使用指针。有时并不清楚writer 也是如此,这会导致错误。因此我建议您更改 Entry类型定义像这样:typedef struct Entry_ Entry;
  • 结构名称末尾的下划线是不必要的,看起来像是一个拼写错误。因此,将其称为 struct Entry 会是更好的风格。 .
  • 在 C 中,无需对 malloc 的结果进行类型转换,这样做可以隐藏失踪#include <stdlib.h>
  • sizeof(char)根据 C 中的定义 1,所以我会将其保留在 malloc 中打电话。

除了这些之外,这个功能对我来说看起来不错。感谢检查和报告错误。

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

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