Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.22 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Valgrind not showing errors with array copy?
  2. #include <algorithm>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   int a[7] = {1, 2, 3, 4, 5, 6, 7};
  9.   int b[7];
  10.   copy(a, a+7, b);
  11.   for (int i=0; i<8; ++i)
  12.     cout << b[i] << endl;
  13. }
  14.        
  15. (gdb) b 1
  16. Breakpoint 1 at 0x100000a64: file stdcopy.cpp, line 1.
  17. (gdb) r
  18. Starting program: /Users/Babai/pastebin/a.out
  19. Reading symbols for shared libraries ++......................... done
  20.  
  21. Breakpoint 1, main () at stdcopy.cpp:7
  22. 7     int a[7] = {1, 2, 3, 4, 5, 6, 7};
  23. (gdb) n
  24. 9     copy(a, a+7, b);
  25. (gdb) s
  26. std::copy<int*, int*> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:398
  27. 398        const bool __in = __is_normal_iterator<_InputIterator>::__value;
  28. (gdb) bt
  29. #0  std::copy<int*, int*> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:398
  30. #1  0x0000000100000acd in main () at stdcopy.cpp:9
  31. (gdb) up
  32. #1  main () at stdcopy.cpp:10
  33. 10    for (int i=0; i<8; ++i)
  34. (gdb) p &a
  35. $1 = (int (*)[7]) 0x7fff5fbffb8c
  36. (gdb) p a + 7
  37. $2 = (int *) 0x7fff5fbffba8
  38.        
  39. #include <algorithm>
  40. #include <iostream>
  41. #include <cstring>
  42.  
  43. int main()
  44. {
  45.   int* a = new int[7];
  46.   int* b = new int[7];
  47.   std::memset(a, 0, sizeof(int) * 7);
  48.   std::memset(b, 0, sizeof(int) * 7);
  49.  
  50.   std::copy(a, a+7, b);
  51.  
  52.   for (int i=0; i<8; ++i)
  53.     std::cout << b[i] << std::endl;
  54.  
  55.   delete[] a;
  56.   delete[] b;
  57. }
  58.        
  59. (gdb) down
  60. #0  std::__copy_aux<int*, int*> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:313
  61. 313                  && __are_same<_ValueTypeI, _ValueTypeO>::__value);
  62. (gdb) n
  63. 315       return std::__copy<__simple, _Category>::copy(__first, __last, __result);
  64. (gdb) s
  65. std::__copy<true, std::random_access_iterator_tag>::copy<int> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:298
  66. 298       std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
  67. (gdb) list
  68. 293     {
  69. 294       template<typename _Tp>
  70. 295         static _Tp*
  71. 296         copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
  72. 297         {
  73. 298       std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
  74. 299       return __result + (__last - __first);
  75. 300     }
  76. 301     };
  77. 302