程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> mbstowcs-高手指教,為什麼這個簡單的函數會報錯呢??

mbstowcs-高手指教,為什麼這個簡單的函數會報錯呢??

編輯:編程綜合問答
高手指教,為什麼這個簡單的函數會報錯呢??

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

最佳回答:


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

zhuwt2008
xueye1986
zhuwt2008
zhuwt2008
oyljerry
zhuwt2008
zhuwt2008
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved