I have the following code:
int main(int argc, char** argv) { char* p = new char[11]; strcpy(p, "1234567890"); cout << strlen(p) << endl; delete[] p; return 0; }
It allocates 11 bytes and then copies a string of 10 bytes plus a nul terminator. It seems to me that this is correct.
But if I run it using Valgrind, I get the following:
bash-4.3$ valgrind ./a.out ... ==44295== Command: ./a.out ==44295== ==44295== Invalid read of size 8 ==44295== at 0x3E6073382F: __strlen_sse42 (in /lib64/libc-2.12.so) ==44295== by 0x4008A9: main (in /bb/mbig_new2/mbig3978/bbgithub/tsacqdata/tsacqdata/unit_test/Cache/a.out) ==44295== Address 0x4c2d048 is 8 bytes inside a block of size 11 alloc'd ==44295== at 0x4A06FE8: operator new[](unsigned long) (vg_replace_malloc.c:363) ==44295== by 0x40087E: main (in /bb/mbig_new2/mbig3978/bbgithub/tsacqdata/tsacqdata/unit_test/Cache/a.out) ...
Why doesn't Valgrind look like this strlen call?
source share