zhuwt2008 2015-04-02 14:44 采纳率: 33.3%
浏览 2087
已采纳

高手指教,为什么这个简单的函数会报错呢??

#include
#include
#include
using namespace std;

wstring w2chs3(const char s1) {
size_t len = strlen(s1);
// wchar_t *ws2 = new wchar_t[len];
wchar_t *ws2 = (wchar_t
)malloc(len*sizeof(wchar_t));
unique_ptr wstr(ws2);
mbstowcs(ws2, s1, 100);
return wstring(ws2);
}

int main() {
auto p2 = w2chs3("hello,world!");
wcout<<p2<<endl;

return 0;

}

不用内存检测工具是运行是正确的,但是用valgrind运行报错,内容:
[root@localhost basecheck]# valgrind --leak-check=yes ./wchartest3
==3086== Memcheck, a memory error detector
==3086== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3086== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==3086== Command: ./wchartest3
==3086==
==3086== Invalid write of size 4
==3086== at 0x5677532: __gconv_transform_ascii_internal (in /usr/lib64/libc-2.17.so)
==3086== by 0x56FCF45: __mbsrtowcs_l (in /usr/lib64/libc-2.17.so)
==3086== by 0x568A4A0: mbstowcs (in /usr/lib64/libc-2.17.so)
==3086== by 0x400CBF: w2chs3(char const*) (wchartest3.cpp:11)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
==3086== Invalid read of size 4
==3086== at 0x56FCF83: __mbsrtowcs_l (in /usr/lib64/libc-2.17.so)
==3086== by 0x568A4A0: mbstowcs (in /usr/lib64/libc-2.17.so)
==3086== by 0x400CBF: w2chs3(char const*) (wchartest3.cpp:11)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
==3086== Invalid read of size 4
==3086== at 0x4C2E404: wcslen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x4EF4074: std::basic_string, std::allocator >::basic_string(wchar_t const*, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.19)
==3086== by 0x400CE2: w2chs3(char const*) (wchartest3.cpp:12)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
==3086== Mismatched free() / delete / delete []
==3086== at 0x4C29E41: operator delete (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x4010AE: std::default_delete::operator()(wchar_t*) const (unique_ptr.h:99)
==3086== by 0x401002: std::unique_ptr >::~unique_ptr() (unique_ptr.h:377)
==3086== by 0x400CFA: w2chs3(char const*) (wchartest3.cpp:12)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086== Address 0x5a12040 is 0 bytes inside a block of size 48 alloc'd
==3086== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==3086== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:9)
==3086== by 0x400D47: main (wchartest3.cpp:16)
==3086==
hello,world!
==3086==
==3086== HEAP SUMMARY:
==3086== in use at exit: 0 bytes in 0 blocks
==3086== total heap usage: 2 allocs, 2 frees, 124 bytes allocated
==3086==
==3086== All heap blocks were freed -- no leaks are possible
==3086==
==3086== For counts of detected and suppressed errors, rerun with: -v
==3086== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 3 from 3)

  • 写回答

9条回答 默认 最新

  • zhuwt2008 2015-04-02 15:20
    关注

    #include cstring
    #include iostream
    #include memory
    using namespace std;

    wstring w2chs3(const char s1) {
    size_t len = strlen(s1);
    wchar_t *ws2 = (wchar_t
    )malloc(len*sizeof(wchar_t));
    mbstowcs(ws2, s1, len);
    wstring wstr(ws2);
    free(ws2);
    return wstr;
    }

    int main() {
    auto p2 = w2chs3("hello,world!");
    wcout<<p2<<endl;

    return 0;
    

    }
    改成这样,剩下一个错误:

    ==2539== Memcheck, a memory error detector
    ==2539== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
    ==2539== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
    ==2539== Command: ./wchartest3
    ==2539==
    ==2539== Invalid read of size 4
    ==2539== at 0x4C2E404: wcslen (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==2539== by 0x4EF4074: std::basic_string, std::allocator >::basic_string(wchar_t const*, std::allocator const&) (in /usr/lib64/libstdc++.so.6.0.19)
    ==2539== by 0x400CCE: w2chs3(char const*) (wchartest3.cpp:10)
    ==2539== by 0x400D27: main (wchartest3.cpp:16)
    ==2539== Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd
    ==2539== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==2539== by 0x400C90: w2chs3(char const*) (wchartest3.cpp:8)
    ==2539== by 0x400D27: main (wchartest3.cpp:16)
    ==2539==
    hello,world!
    ==2539==
    ==2539== HEAP SUMMARY:
    ==2539== in use at exit: 0 bytes in 0 blocks
    ==2539== total heap usage: 2 allocs, 2 frees, 124 bytes allocated
    ==2539==
    ==2539== All heap blocks were freed -- no leaks are possible
    ==2539==
    ==2539== For counts of detected and suppressed errors, rerun with: -v
    ==2539== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 3)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?