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

标签 c++ memory-leaks valgrind

这些是我认为问题所在的几个片段(否则有很多代码):

//to insert the course by name and number into the hash table
int Table::insert(Course & c) {
  return insertByName(c) - insertByNumber(c); //return 0 for success
}


//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;
  }
  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;
  }
  return 1;
}

更新:这是Course::getName()Course::getNumber():

//to copy the name of the course to the passed array
int Course::getName(char *& arr) {
  arr = new char[strlen(name) + 1];
  strcpy(arr, name);
  return 1;
}


//to copy the number of the course to the passed array
int Course::getNumber(char *& arr) {
  arr = new char[strlen(number) + 1];
  strcpy(arr, number);
  return 1;
}

这是详细的错误消息 (Valgrind):

--24010-- Reading syms from /lib/x86_64-linux-gnu/ld-2.19.so
--24010--   Considering /lib/x86_64-linux-gnu/ld-2.19.so ..
--24010--   .. CRC mismatch (computed ef2bc4a1 wanted 12987a55)
--24010--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.19.so ..
--24010--   .. CRC is valid
--24010-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux
--24010--   Considering /usr/lib/valgrind/memcheck-amd64-linux ..
--24010--   .. CRC mismatch (computed 4f1eed43 wanted a323a3ab)
--24010--    object doesn't have a symbol table
--24010--    object doesn't have a dynamic symbol table
--24010-- Scheduler: using generic scheduler lock implementation.
--24010-- Reading suppressions file: /usr/lib/valgrind/default.supp
==24010== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-24010-by-bad6-on-???
==24010== embedded gdbserver: writing to   /tmp/vgdb-pipe-to-vgdb-from-24010-by-bad6-on-???
==24010== embedded gdbserver: shared mem   /tmp/vgdb-pipe-shared-mem-vgdb-24010-by-bad6-on-???
==24010== 
==24010== TO CONTROL THIS PROCESS USING vgdb (which you probably
==24010== don't want to do, unless you know exactly what you're doing,
==24010== or are doing some strange experiment):
==24010==   /usr/lib/valgrind/../../bin/vgdb --pid=24010 ...command...
==24010== 
==24010== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==24010==   /path/to/gdb ./a.out
==24010== and then give GDB the following command
==24010==   target remote | /usr/lib/valgrind/../../bin/vgdb --pid=24010
==24010== --pid is optional if only one valgrind process is running
==24010== 
--24010-- REDIR: 0x4019ca0 (ld-linux-x86-64.so.2:strlen) redirected to 0x380764b1 (???)
--24010-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so
--24010--   Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
--24010--   .. CRC mismatch (computed fc68135e wanted 45f5e986)
--24010--    object doesn't have a symbol table
--24010-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
--24010--   Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
--24010--   .. CRC mismatch (computed ae683f7e wanted 08c06df2)
--24010--    object doesn't have a symbol table
==24010== WARNING: new redirection conflicts with existing -- ignoring it
--24010--     old: 0x04019ca0 (strlen              ) R-> (0000.0) 0x380764b1 ???
--24010--     new: 0x04019ca0 (strlen              ) R-> (2007.0) 0x04c2e1a0 strlen
--24010-- REDIR: 0x4019a50 (ld-linux-x86-64.so.2:index) redirected to 0x4c2dd50 (index)
--24010-- REDIR: 0x4019c70 (ld-linux-x86-64.so.2:strcmp) redirected to 0x4c2f2f0 (strcmp)
--24010-- REDIR: 0x401a9c0 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x4c31da0 (mempcpy)
--24010-- Reading syms from /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
--24010--   Considering /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19 ..
--24010--   .. CRC mismatch (computed 5ce96d3c wanted bf86fabd)
--24010--    object doesn't have a symbol table
--24010-- Reading syms from /lib/x86_64-linux-gnu/libgcc_s.so.1
--24010--   Considering /lib/x86_64-linux-gnu/libgcc_s.so.1 ..
--24010--   .. CRC mismatch (computed 6116126e wanted 54e3f1f2)
--24010--    object doesn't have a symbol table
--24010-- Reading syms from /lib/x86_64-linux-gnu/libc-2.19.so
--24010--   Considering /lib/x86_64-linux-gnu/libc-2.19.so ..
--24010--   .. CRC mismatch (computed 600bae51 wanted b4d0580d)
--24010--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so ..
--24010--   .. CRC is valid
--24010-- Reading syms from /lib/x86_64-linux-gnu/libm-2.19.so
--24010--   Considering /lib/x86_64-linux-gnu/libm-2.19.so ..
--24010--   .. CRC mismatch (computed 0fbb5cf0 wanted cac31e3b)
--24010--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/libm-2.19.so ..
--24010--   .. CRC is valid
--24010-- REDIR: 0x53ddd60 (libc.so.6:strcasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--24010-- REDIR: 0x53e0050 (libc.so.6:strncasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--24010-- REDIR: 0x53dd530 (libc.so.6:memcpy@GLIBC_2.2.5) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--24010-- REDIR: 0x53db7c0 (libc.so.6:rindex) redirected to 0x4c2da30 (rindex)
--24010-- REDIR: 0x53d9ac0 (libc.so.6:strlen) redirected to 0x4c2e0e0 (strlen)
--24010-- REDIR: 0x53dcfa0 (libc.so.6:__GI_memcmp) redirected to 0x4c30b80 (__GI_memcmp)
--24010-- REDIR: 0x53d8070 (libc.so.6:strcmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--24010-- REDIR: 0x53e8d20 (libc.so.6:__strcmp_sse2_unaligned) redirected to 0x4c2f1b0 (strcmp)
--24010-- REDIR: 0x4e95ea0 (libstdc++.so.6:operator new[](unsigned long)) redirected to 0x4c2b790 (operator new[](unsigned long))
# How many courses would you like to enter? 1
- Please enter the name of the course: a
--24010-- REDIR: 0x53d9500 (libc.so.6:strcpy) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--24010-- REDIR: 0x53edb90 (libc.so.6:__strcpy_sse2_unaligned) redirected to 0x4c2e1c0 (strcpy)
- Please enter the number of the course: a
- Please enter the section of the course: 
- Please enter the time of the course: a
- Please enter the CRN of the course: a
- Please enter the description of the course: a
===============================================================
--24010-- REDIR: 0x4e94120 (libstdc++.so.6:operator delete[](void*)) redirected to 0x4c2c7d0 (operator delete[](void*))
==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)
==24010== 
==24010== Use of uninitialised value of size 8
==24010==    at 0x4023B9: Table::insertByName(Course&) (Table.cpp:146)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==24010== Use of uninitialised value of size 8
==24010==    at 0x4023E0: Table::insertByName(Course&) (Table.cpp:147)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==24010== Use of uninitialised value of size 8
==24010==    at 0x4024E3: Table::insertByNumber(Course&) (Table.cpp:167)
==24010==    by 0x401DF2: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==24010== Use of uninitialised value of size 8
==24010==    at 0x4025A2: Table::insertByNumber(Course&) (Table.cpp:174)
==24010==    by 0x401DF2: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==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)
==24010== 
--24010-- REDIR: 0x4e940f0 (libstdc++.so.6:operator delete(void*)) redirected to 0x4c2c250 (operator delete(void*))
--24010-- REDIR: 0x53d3df0 (libc.so.6:free) redirected to 0x4c2bd80 (free)
==24010== 
==24010== HEAP SUMMARY:
==24010==     in use at exit: 4 bytes in 2 blocks
==24010==   total heap usage: 29 allocs, 27 frees, 456 bytes allocated
==24010== 
==24010== Searching for pointers to 2 not-freed blocks
==24010== Checked 194,168 bytes
==24010== 
==24010== LEAK SUMMARY:
==24010==    definitely lost: 4 bytes in 2 blocks
==24010==    indirectly lost: 0 bytes in 0 blocks
==24010==      possibly lost: 0 bytes in 0 blocks
==24010==    still reachable: 0 bytes in 0 blocks
==24010==         suppressed: 0 bytes in 0 blocks
==24010== Rerun with --leak-check=full to see details of leaked memory
==24010== 
==24010== Use --track-origins=yes to see where uninitialised values come from
==24010== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 0 from 0)
==24010== 
==24010== 1 errors in context 1 of 7:
==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)
==24010== 
==24010== 
==24010== 1 errors in context 2 of 7:
==24010== Use of uninitialised value of size 8
==24010==    at 0x4025A2: Table::insertByNumber(Course&) (Table.cpp:174)
==24010==    by 0x401DF2: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==24010== 
==24010== 1 errors in context 3 of 7:
==24010== Use of uninitialised value of size 8
==24010==    at 0x4024E3: Table::insertByNumber(Course&) (Table.cpp:167)
==24010==    by 0x401DF2: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==24010== 
==24010== 1 errors in context 4 of 7:
==24010== Use of uninitialised value of size 8
==24010==    at 0x4023E0: Table::insertByName(Course&) (Table.cpp:147)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==24010== 
==24010== 1 errors in context 5 of 7:
==24010== Use of uninitialised value of size 8
==24010==    at 0x4023B9: Table::insertByName(Course&) (Table.cpp:146)
==24010==    by 0x401DDD: Table::insert(Course&) (Table.cpp:56)
==24010==    by 0x40144C: main (main.cpp:39)
==24010== 
==24010== 
==24010== 1 errors in context 6 of 7:
==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)
==24010== 
==24010== 
==24010== 1 errors in context 7 of 7:
==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== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 0 from 0)

有没有人知道错误是什么?我希望有人能用这么少的信息“理解”这个问题,因为我认为这会更容易,因为我有 1000 多行代码被分成不同的头文件和 .cpp 文件。

谢谢

最佳答案

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,要么可能是 Index 超出范围。

--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;

很难看出是什么引起了这里的提示——如果 node 有一个构造函数,这可能有一个错误,否则 table[index] 正在初始化,这可能是一个误报

关于c++ - 使用大小为 8 的未初始化值。C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35714853/

相关文章:

C++ Boost ASIO async_send_to 内存泄漏

Valgrind 与 Linux 性能相关性

c++ - PHP 扩展 - 使用 std::string 的全局类

c++ - 是否有即时编译的正则表达式引擎?

c# - dotMemory 和跟踪内存泄漏

c - C内存读写BMP文件错误

c++ - 为什么我在使用 valgrind 时会出现内存错误? (C++,抽象语法树求值)

c++ - 重载方法未在强制转换运算符重载中调用

c++ - 哪些 iomanip 操纵器是 'sticky' ?

c++ - boost 日志文件未写入