Mande1993
Mande1993

Reputation: 21

Use of uninitialised value of size 8. C++

These are few snippets of where I think the problem lies (a lot of code otherwise):

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

UPDATE: Here is Course::getName() and 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;
}

HERE IS A DETAILED ERROR MESSAGE (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)

Does anyone have an idea of what the error is about? I hope someone would "understand" the problem with this little amount of information, because I thought it would be easier since I have more than 1000 lines of code separated into different header files and .cpp files.

Thanks

Upvotes: 1

Views: 6579

Answers (3)

mksteve
mksteve

Reputation: 13073

UPDATE

I don't think this analysis is correct, as I mis-read the question. But I think I helped the OP, with this answer whilst re-working.

As such I have un-deleted - in case it helps other people.

The complaint is

int Course::getName(char *& arr) {
  arr = new char[strlen(name) + 1];
  strcpy(arr, name);
  return 1;
}

That the global/member name Does not yet have a value. This would be better as a parameter. Similar issue in number

==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)

The display describes a call stack.

main => Table::insert => Table::insertByNumber

where it sees an issue. So line 175 of Table.cpp is the line of the file which causes this.

Looking at arr, I can see it is only used on the left, and appears to have a value. That means the thing on the right (name or number) must be the cause.

The request for line numbers in comments was important, because it helps identify from possible causes.

Upvotes: 0

mksteve
mksteve

Reputation: 13073

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

relates to

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

It either thinks that table array was not fully initialized, so the value in Table might not be NULL, or it may possibly be Index is out of range.

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

Its difficult to see what is causing the complaint here - if node has a constructor, this may have an error, otherwise table[index] is being initialized, and this may be a mis-report

Upvotes: 2

Mande1993
Mande1993

Reputation: 21

Thanks to @mksteve, I managed to know where to look exactly and I found that it was not an initialization problem!!! What I did wrong was that I didn't deallocate the memory used to for the temp array used inside Course::getName() and Course::getNumber().

Here is the solution:

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

Thank you all for your support. I think the problem was so clear, but I wasn't looking at the right place. I also think that the error message was a little bizarre, don't you think. Anyway, thanks again.

Upvotes: 1

Related Questions