使用 wmemset 初始化导致核心转储
Use wmemset initialize cause coredump
我想将int
值转换为wchar_t buffer[MAX_LEN]
,使用std::to_wstring(dwValue);
然后使用res.copy
将值复制到缓冲区,我不知道为什么会导致核心转储。如果我wmemset
线,一切都很顺利。
我使用gdb
和valgrind
,找不到任何理由。这是valgrind
结果:
==30757== Jump to the invalid address stated on the next line
==30757== at 0x0: ???
==30757== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==30757==
==30757==
==30757== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==30757== Bad permissions for mapped region at address 0x0
==30757== at 0x0: ???
==30757==
==30757== HEAP SUMMARY:
==30757== in use at exit: 0 bytes in 0 blocks
==30757== total heap usage: 1 allocs, 1 frees, 44 bytes allocated
==30757==
==30757== All heap blocks were freed -- no leaks are possible
==30757==
==30757== For counts of detected and suppressed errors, rerun with: -v
==30757== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault
我的代码如下:
/* intTowstring example */
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cwchar>
std::wstring intToWstring( int dwValue )
{
std::wstring str = L"";
str = std::to_wstring(dwValue);
return str;
}
int main ()
{
wchar_t buffer[128];
wmemset (buffer,L' ',sizeof(wchar_t) * 128);
int flags = 2048;
std::wstring res = intToWstring(flags);
res.copy(buffer,res.size(),0);
wprintf(L"%lsn",buffer);
return 0;
}
wmemset (buffer,L' ',sizeof(wchar_t) * 128);
我想这是错误的。从wmemset
文档中:
count
- 要填充的宽字符数
您正在提供多个字节。更改为:
wmemset (buffer,L' ',128);
快速
浏览一下wmemset
的文档就会发现,最后一个参数应该是元素的数量,而不是字节的数量。
引用文档:
wchar_t* wmemset( wchar_t* dest, wchar_t ch, std::size_t count );
Parameters
dest - pointer to the wide character array to fill
ch - fill wide character
count - number of wide characters to fill
解决方案是将呼叫更改为wmemset
wmemset (buffer,L' ',128);
参考
相关文章:
- 如何找出GDB的SIGTRAP核心转储的根本原因
- C++映射分割错误(核心转储)
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 浮点异常(核心转储)#694457
- 分段错误(核心转储)但无法弄清楚
- 链接到libkcapi时没有核心转储
- 分段错误(核心转储) - 使用 SavedModel 的 Tensorflow C++ API 进行推断
- 在基数排序中,我得到 munmap_chunk():无效指针和中止(核心转储).为什么?
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 合并排序:分段错误核心转储
- C++:分段错误(核心转储)
- 从 pybind11 构建导入模块时出现"分段错误(核心转储)"
- 如何使用 lldb 在曼扎罗中调试 lz4 文件格式的核心转储?
- qt C++命令行代码中的分段错误(核心转储)
- 从文件中读取的数据,但发生核心转储错误
- googletest:测试基类具有纯虚拟方法的派生类时的核心转储
- C++ atof - 分段错误(核心转储)
- Linux C 只禁止我的程序使用核心转储
- 为什么我的代码包含错误分段错误(核心转储)?