The valgrind reports error when printing allocated strings

那年仲夏 提交于 2019-12-01 05:10:54
peter

This is a bug, but not reproducible on all machines.

On some machines, gcc optimizes simple printf() with, for example, puts(), which could possibly involve invalid read (or just valgrind thinks so).

If it really matters, you can 'complicate' the printf format. A space between %s and \n would do.

Here is a similar bug: C strings, strlen and Valgrind

This answer combines comments in the discussion. Thank you all!

I've run it in my own machine, and I get no errors:

==61755== Memcheck, a memory error detector
==61755== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==61755== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==61755== Command: ./a.out
==61755==
hi
==61755==
==61755== HEAP SUMMARY:
==61755==     in use at exit: 0 bytes in 0 blocks
==61755==   total heap usage: 1 allocs, 1 frees, 3 bytes allocated
==61755==
==61755== All heap blocks were freed -- no leaks are possible
==61755==
==61755== For counts of detected and suppressed errors, rerun with: -v
==61755== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!