Valgrind错误与新数组

时间:2018-03-13 16:47:55

标签: c++ valgrind strlen

我有以下代码:

int main(int argc, char** argv)
{
    char* p = new char[11];
    strcpy(p, "1234567890");
    cout << strlen(p) << endl;
    delete[] p;
    return 0;
}

它分配11个字节,然后复制一个10字节的字符串加上一个nul终结符。这对我来说似乎很正确。

但如果我用Valgrind运行它,我会得到这个:

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

为什么Valgrind不喜欢那个strlen来电?

0 个答案:

没有答案