这些是我认为问题所在的几个片段(否则会有很多代码):
//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文件。
由于
答案 0 :(得分:1)
感谢@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;
}
谢谢大家的支持。我认为这个问题非常清楚,但我并没有找到合适的地方。我也认为错误信息有点离奇,你不觉得。无论如何,再次感谢。
答案 1 :(得分: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==
它认为表数组未完全初始化,因此Table中的值可能不是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
有构造函数,这可能有错误,否则表[index]正在初始化,这可能是错误的报告
答案 2 :(得分:0)
我不认为这种分析是正确的,因为我误读了这个问题。但我认为我在重新工作的同时帮助了OP,并给出了答案。
因此我没有删除 - 如果它可以帮助其他人。
投诉是
int Course::getName(char *& arr) {
arr = new char[strlen(name) + 1];
strcpy(arr, name);
return 1;
}
全球/成员name
还没有价值。这可以作为参数更好。
数字中的类似问题
==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
)必须是原因。
评论中的行号请求很重要,因为它有助于识别可能的原因。