C++ 使用大小为8的未初始化值。C++;

C++ 使用大小为8的未初始化值。C++;,c++,memory-leaks,valgrind,C++,Memory Leaks,Valgrind,以下是我认为问题所在的几个片段(除此之外还有很多代码): 更新:这里是课程::getName()和课程::getNumber(): 下面是一条详细的错误消息(Valgrind): //to insert the course by the name into the hash table int Table::insertByName(Course & c) { int index; char *name; c.getName(name); //copy the name

以下是我认为问题所在的几个片段(除此之外还有很多代码):

更新:这里是
课程::getName()
课程::getNumber()

下面是一条详细的错误消息(Valgrind):

 //to insert the course by the name into the hash table
int Table::insertByName(Course & c) {
  int index;
  char *name;

  c.getName(name); //copy the name of the course
  index = hashFunc(name); //get the index for the name
  //insert the course by the index of the name at head
  if (table[index] == NULL) { ///////////////////////////// LINE: 144
      table[index] = new node;
      table[index]->course.copy(c);
      table[index]->next = NULL;
  } else {
      node * temp = new node;
      temp->course.copy(c);
      temp->next = table[index];
      table[index] = temp;
  }
  delete[] name; ///////////////////////////////////THAT FIXED THE BUG
  return 1;
}



//to insert the course by the number into the hash table
  int Table::insertByNumber(Course & c) {
  int index;
  char *number;

  c.getNumber(number); //copy the number of the course
  index = hashFunc(number); //get the index for the number

  //insert the course by the index of the name at head
  if (table[index] == NULL) { ///////////////////////////// LINE: 167
      table[index] = new node;
      table[index]->course.copy(c);
      table[index]->next = NULL;
  } else {
      node * temp = new node;
      temp->course.copy(c);
      temp->next = table[index];
      table[index] = temp;
  }
  delete[] number; ///////////////////////////////////THAT FIXED THE BUG
  return 1;
}
有人知道错误是怎么回事吗?我希望有人能“理解”这一点信息的问题,因为我认为这会更容易,因为我有1000多行代码分为不同的头文件和.cpp文件

谢谢更新 我不认为这个分析是正确的,因为我看错了这个问题。但我想我帮助了OP,在重新工作的时候给出了这个答案

因此,我已取消删除-以防它帮助其他人

投诉是

int Course::getName(char *& arr) {
  arr = new char[strlen(name) + 1];
  strcpy(arr, name);
  return 1;
}
全局/成员
名称
还没有一个值。这最好作为一个参数。 数量相似

==24010== Use of uninitialised value of size 8
==24010==    at 0x4025C6: Table::insertByNumber(Course&) (Table.cpp:175)
==24010==    by 0x401DF2: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
显示屏描述了一个调用堆栈

main => Table::insert => Table::insertByNumber
它看到问题的地方。因此,Table.cpp的第175行是导致此问题的文件行

查看
arr
,我可以看到它仅在左侧使用,似乎有一个值。这意味着右边的东西(
name
number
)一定是原因


请求注释中的行号很重要,因为它有助于识别可能的原因。

多亏了@mksteve,我成功地知道了确切的查找位置,我发现这不是一个初始化问题。!!!我做错的是没有释放用于
Course::getName()
Course::getNumber()
中使用的临时数组的内存

解决方案如下:

 //to insert the course by the name into the hash table
int Table::insertByName(Course & c) {
  int index;
  char *name;

  c.getName(name); //copy the name of the course
  index = hashFunc(name); //get the index for the name
  //insert the course by the index of the name at head
  if (table[index] == NULL) { ///////////////////////////// LINE: 144
      table[index] = new node;
      table[index]->course.copy(c);
      table[index]->next = NULL;
  } else {
      node * temp = new node;
      temp->course.copy(c);
      temp->next = table[index];
      table[index] = temp;
  }
  delete[] name; ///////////////////////////////////THAT FIXED THE BUG
  return 1;
}



//to insert the course by the number into the hash table
  int Table::insertByNumber(Course & c) {
  int index;
  char *number;

  c.getNumber(number); //copy the number of the course
  index = hashFunc(number); //get the index for the number

  //insert the course by the index of the name at head
  if (table[index] == NULL) { ///////////////////////////// LINE: 167
      table[index] = new node;
      table[index]->course.copy(c);
      table[index]->next = NULL;
  } else {
      node * temp = new node;
      temp->course.copy(c);
      temp->next = table[index];
      table[index] = temp;
  }
  delete[] number; ///////////////////////////////////THAT FIXED THE BUG
  return 1;
}
谢谢大家的支持。我认为问题很明显,但我没有看到正确的地方。我还认为错误信息有点奇怪,你不觉得吗。无论如何,再次谢谢你

if (table[index] == NULL) { ///////////////////////////// LINE: 144
涉及

==24010== Use of uninitialised value of size 8
==24010==    at 0x40236F: Table::insertByName(Course&) (Table.cpp:144)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
它可能认为表数组未完全初始化,因此表中的值可能不为NULL,也可能是
索引
超出范围

--24010-- REDIR: 0x4e95d90 (libstdc++.so.6:operator new(unsigned long)) redirected to 0x4c2b070 (operator new(unsigned long))
==24010== Use of uninitialised value of size 8
==24010==    at 0x4023A1: Table::insertByName(Course&) (Table.cpp:145)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)

/*145*/  table[index] = new node;

很难看出是什么导致了这里的投诉-如果
节点
有一个构造函数,这可能有一个错误,否则表[index]正在初始化,这可能是一个错误报告

我认为错误不在您共享的代码段中。请参考此链接了解错误:请发布
Course::getName()
Course::getNumber()
的代码。另外,你能标记行号吗?特别是第144行和第167行。@AntonSavin I添加了它们,使用valgrind做得很好。实际上你又对了!!!!我解决了一个存在的问题,但仍然存在一些与初始化相关的错误。我会继续找的。我强烈感觉它是表的复制构造函数(因为我在程序中通过值传递它)。如果我把它贴在这里会有帮助吗?而且,我不认为这是一个越界的问题,因为我使用了gdb,而且它没有seg错误。节点没有构造函数(它是一个结构)。
==24010== Use of uninitialised value of size 8
==24010==    at 0x40236F: Table::insertByName(Course&) (Table.cpp:144)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
--24010-- REDIR: 0x4e95d90 (libstdc++.so.6:operator new(unsigned long)) redirected to 0x4c2b070 (operator new(unsigned long))
==24010== Use of uninitialised value of size 8
==24010==    at 0x4023A1: Table::insertByName(Course&) (Table.cpp:145)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)

/*145*/  table[index] = new node;