- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 strtod() 来转换一些输入字符串。在用 valgrind 检查我的代码时,我遇到了“大小为 8 的无效读取”。如果 b 以“i”或“n”开头,则会显示该消息,这就是我到目前为止所发现的。另外,如果我直接创建一个 const char*(不调用 c_str()),strtod 似乎也可以。这是代码:
#include <cstdlib>
#include <string>
int main(int argc, char** argv) {
char* a = 0;
std::string b = "i";
const char* c = b.c_str();
double d = strtod(c, &a);
}
和(详细的)valgrind 输出:
==12638== Memcheck, a memory error detector
==12638== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==12638== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==12638== Command: ./leak
==12638==
--12638-- Valgrind options:
--12638-- --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--12638-- --show-reachable=yes
--12638-- --leak-check=full
--12638-- -v
--12638-- Contents of /proc/version:
--12638-- Linux version 3.2.0-24-generic (buildd@yellow) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #37-Ubuntu SMP Wed Apr 25 08:43:22 UTC 2012
--12638-- Arch and hwcaps: AMD64, amd64-sse3-cx16
--12638-- Page sizes: currently 4096, max supported 4096
--12638-- Valgrind library directory: /usr/lib/valgrind
--12638-- Reading syms from /media/userdata/NetBeansProjects/leak/dist/Debug/GNU-Linux-x86/leak (0x400000)
--12638-- Reading syms from /lib/x86_64-linux-gnu/ld-2.15.so (0x4000000)
--12638-- Considering /lib/x86_64-linux-gnu/ld-2.15.so ..
--12638-- .. CRC mismatch (computed c5d70380 wanted 7d4a76d0)
--12638-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.15.so ..
--12638-- .. CRC is valid
--12638-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux (0x38000000)
--12638-- Considering /usr/lib/valgrind/memcheck-amd64-linux ..
--12638-- .. CRC mismatch (computed b9a585cc wanted 749d1a67)
--12638-- object doesn't have a symbol table
--12638-- object doesn't have a dynamic symbol table
--12638-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--12638-- Reading suppressions file: /usr/lib/valgrind/default.supp
==12638== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-12638-by-chri-on-???
==12638== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-12638-by-chri-on-???
==12638== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-12638-by-chri-on-???
==12638==
==12638== TO CONTROL THIS PROCESS USING vgdb (which you probably
==12638== don't want to do, unless you know exactly what you're doing,
==12638== or are doing some strange experiment):
==12638== /usr/lib/valgrind/../../bin/vgdb --pid=12638 ...command...
==12638==
==12638== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==12638== /path/to/gdb ./leak
==12638== and then give GDB the following command
==12638== target remote | /usr/lib/valgrind/../../bin/vgdb --pid=12638
==12638== --pid is optional if only one valgrind process is running
==12638==
--12638-- REDIR: 0x40189e0 (strlen) redirected to 0x380625c7 (???)
--12638-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so (0x4a25000)
--12638-- Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
--12638-- .. CRC mismatch (computed c82927cb wanted 1861273b)
--12638-- object doesn't have a symbol table
--12638-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so (0x4c27000)
--12638-- Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
--12638-- .. CRC mismatch (computed 72e29ec9 wanted f3ad49da)
--12638-- object doesn't have a symbol table
--12638-- REDIR: 0x4018850 (index) redirected to 0x4c2bc60 (index)
--12638-- REDIR: 0x40188d0 (strcmp) redirected to 0x4c2cc20 (strcmp)
--12638-- Reading syms from /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16 (0x4e32000)
--12638-- Considering /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16 ..
--12638-- .. CRC mismatch (computed ef748751 wanted e44c0328)
--12638-- object doesn't have a symbol table
--12638-- Reading syms from /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x5132000)
--12638-- Considering /lib/x86_64-linux-gnu/libgcc_s.so.1 ..
--12638-- .. CRC mismatch (computed c8e4b229 wanted 098e39f6)
--12638-- object doesn't have a symbol table
--12638-- Reading syms from /lib/x86_64-linux-gnu/libc-2.15.so (0x5348000)
--12638-- Considering /lib/x86_64-linux-gnu/libc-2.15.so ..
--12638-- .. CRC mismatch (computed 0dfc7f92 wanted 306696b6)
--12638-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.15.so ..
--12638-- .. CRC is valid
--12638-- Reading syms from /lib/x86_64-linux-gnu/libm-2.15.so (0x5705000)
--12638-- Considering /lib/x86_64-linux-gnu/libm-2.15.so ..
--12638-- .. CRC mismatch (computed 84e90409 wanted 8a8fcc58)
--12638-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/libm-2.15.so ..
--12638-- .. CRC is valid
--12638-- REDIR: 0x53d48c0 (strcasecmp) redirected to 0x4a25610 (_vgnU_ifunc_wrapper)
--12638-- REDIR: 0x53d0c60 (strnlen) redirected to 0x4a25610 (_vgnU_ifunc_wrapper)
--12638-- REDIR: 0x53d6b90 (strncasecmp) redirected to 0x4a25610 (_vgnU_ifunc_wrapper)
--12638-- REDIR: 0x53d2650 (__GI_strrchr) redirected to 0x4c2ba80 (__GI_strrchr)
--12638-- REDIR: 0x53d0b30 (strlen) redirected to 0x4a25610 (_vgnU_ifunc_wrapper)
--12638-- REDIR: 0x54a93d0 (__strlen_sse2_pminub) redirected to 0x4c2bfa0 (strlen)
--12638-- REDIR: 0x4ee7dd0 (operator new(unsigned long)) redirected to 0x4c2b160 (operator new(unsigned long))
--12638-- REDIR: 0x53d0b80 (__GI_strlen) redirected to 0x4c2bfc0 (__GI_strlen)
==12638== Invalid read of size 8
==12638== at 0x53D6C4F: __GI___strncasecmp_l (strcmp.S:213)
==12638== by 0x538900A: ____strtod_l_internal (strtod_l.c:566)
==12638== by 0x40088B: main (main.cpp:9)
==12638== Address 0x59ff058 is 24 bytes inside a block of size 26 alloc'd
==12638== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12638== by 0x4ED0A88: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED2494: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED25E2: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x40085C: main (main.cpp:7)
==12638==
==12638== Invalid read of size 8
==12638== at 0x53D6C57: __GI___strncasecmp_l (strcmp.S:215)
==12638== by 0x538900A: ____strtod_l_internal (strtod_l.c:566)
==12638== by 0x40088B: main (main.cpp:9)
==12638== Address 0x59ff060 is 6 bytes after a block of size 26 alloc'd
==12638== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12638== by 0x4ED0A88: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED2494: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED25E2: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x40085C: main (main.cpp:7)
==12638==
--12638-- REDIR: 0x4ee6150 (operator delete(void*)) redirected to 0x4c2a450 (operator delete(void*))
--12638-- REDIR: 0x53cb010 (free) redirected to 0x4c2a7c0 (free)
==12638==
==12638== HEAP SUMMARY:
==12638== in use at exit: 0 bytes in 0 blocks
==12638== total heap usage: 1 allocs, 1 frees, 26 bytes allocated
==12638==
==12638== All heap blocks were freed -- no leaks are possible
==12638==
==12638== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)
==12638==
==12638== 1 errors in context 1 of 2:
==12638== Invalid read of size 8
==12638== at 0x53D6C57: __GI___strncasecmp_l (strcmp.S:215)
==12638== by 0x538900A: ____strtod_l_internal (strtod_l.c:566)
==12638== by 0x40088B: main (main.cpp:9)
==12638== Address 0x59ff060 is 6 bytes after a block of size 26 alloc'd
==12638== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12638== by 0x4ED0A88: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED2494: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED25E2: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x40085C: main (main.cpp:7)
==12638==
==12638==
==12638== 1 errors in context 2 of 2:
==12638== Invalid read of size 8
==12638== at 0x53D6C4F: __GI___strncasecmp_l (strcmp.S:213)
==12638== by 0x538900A: ____strtod_l_internal (strtod_l.c:566)
==12638== by 0x40088B: main (main.cpp:9)
==12638== Address 0x59ff058 is 24 bytes inside a block of size 26 alloc'd
==12638== at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12638== by 0x4ED0A88: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED2494: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x4ED25E2: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
==12638== by 0x40085C: main (main.cpp:7)
==12638==
--12638--
--12638-- used_suppression: 2 dl-hack3-cond-1
==12638==
==12638== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)
是否有错误或我遗漏了什么?
最佳答案
我认为您的代码中没有错误。我认为这是 Valgrind 中的错误。
Valgrind 有自己的函数实现,例如 strchr
、strcasecmp
等,它会重定向您的应用程序使用,而不是 libgcc 中的版本。 Valgrind 中的评论 mc_replace_strmem.c
文件说明:
/* ---------------------------------------------------------------------
We have our own versions of these functions for two reasons:
(a) it allows us to do overlap checking
(b) some of the normal versions are hyper-optimised, which fools
Memcheck and cause spurious value warnings. Our versions are
simpler.
Note that overenthusiastic use of PLT bypassing by the glibc people also
means that we need to patch multiple versions of some of the functions to
our own implementations.
THEY RUN ON THE SIMD CPU!
------------------------------------------------------------------ */
您可以在日志的 REDIR
输出中看到这些重定向。
从 mc_replace_strmem.c
的检查来看,Valgrind 似乎没有替换 __GI___strncasecmp_l
,您的日志表明它正在被调用。我建议 filing a bug针对 Valgrind 并将您的程序作为测试用例。
关于c++ - Valgrind: "Invalid read"与 c_str 和 strtod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10572715/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!