
Untitled
By: a guest on
May 1st, 2012 | syntax:
None | size: 2.22 KB | hits: 14 | expires: Never
Valgrind not showing errors with array copy?
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7};
int b[7];
copy(a, a+7, b);
for (int i=0; i<8; ++i)
cout << b[i] << endl;
}
(gdb) b 1
Breakpoint 1 at 0x100000a64: file stdcopy.cpp, line 1.
(gdb) r
Starting program: /Users/Babai/pastebin/a.out
Reading symbols for shared libraries ++......................... done
Breakpoint 1, main () at stdcopy.cpp:7
7 int a[7] = {1, 2, 3, 4, 5, 6, 7};
(gdb) n
9 copy(a, a+7, b);
(gdb) s
std::copy<int*, int*> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:398
398 const bool __in = __is_normal_iterator<_InputIterator>::__value;
(gdb) bt
#0 std::copy<int*, int*> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:398
#1 0x0000000100000acd in main () at stdcopy.cpp:9
(gdb) up
#1 main () at stdcopy.cpp:10
10 for (int i=0; i<8; ++i)
(gdb) p &a
$1 = (int (*)[7]) 0x7fff5fbffb8c
(gdb) p a + 7
$2 = (int *) 0x7fff5fbffba8
#include <algorithm>
#include <iostream>
#include <cstring>
int main()
{
int* a = new int[7];
int* b = new int[7];
std::memset(a, 0, sizeof(int) * 7);
std::memset(b, 0, sizeof(int) * 7);
std::copy(a, a+7, b);
for (int i=0; i<8; ++i)
std::cout << b[i] << std::endl;
delete[] a;
delete[] b;
}
(gdb) down
#0 std::__copy_aux<int*, int*> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:313
313 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
(gdb) n
315 return std::__copy<__simple, _Category>::copy(__first, __last, __result);
(gdb) s
std::__copy<true, std::random_access_iterator_tag>::copy<int> (__first=0x7fff5fbffb8c, __last=0x7fff5fbffba8, __result=0x7fff5fbffb70) at stl_algobase.h:298
298 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
(gdb) list
293 {
294 template<typename _Tp>
295 static _Tp*
296 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
297 {
298 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
299 return __result + (__last - __first);
300 }
301 };
302