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)

解决方案二:

上面头文件没有了,补充::
#include <cstring>
#include <iostream>
#include <memory>

解决方案三:

wstring w2chs3(const char s1) ?你确定你没写错这里么?

解决方案四:

malloc 得到的内存,没有自己释放吧,所以你用内存检测工具会报错!

解决方案五:

奇怪,为什么wstring w2chs3(const char s1) {这一行中的星号*也没有了,本来是有的,改正一下
wstring w2chs3(const char *s1) {

解决方案六:

malloc的ws2没有对应的free,造成内存泄露。

解决方案七:

我将程序改成这样,用new,智能指针应该会正确调用delete[],但还是报错:
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, len);
return wstring(ws2);
}

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

return 0;

}

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

解决方案八:

我将程序改成这样,用new,智能指针应该会正确调用delete[],但还是报错:
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, len);
return wstring(ws2);
}

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

return 0;

}

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

解决方案九:

我改成这样也不行
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);
free(ws2);
return wstring(ws2);
}

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

return 0;

}

解决方案十:

#include
#include
#include
#include
using namespace std;

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

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

return 0;

}

你看看吧 贴出来的代码 都编译不过让别人怎么帮你看问题。如果还报错,应该是linux下malloc的内存不能用delete释放,你不要用智能指针了,老老实实的用freee吧。

时间: 2024-12-23 19:05:37

mbstowcs-高手指教,为什么这个简单的函数会报错呢??的相关文章

c-这个非常简单的代码,为啥用了函数就报错呢

问题描述 这个非常简单的代码,为啥用了函数就报错呢 题目是:输入n,在输入n个数<(2∧31-1)求这n个数的平方和 我的代码: #include #include #include #include #include using namespace std; long long sum(long long data,int n) { long long ans; ans=0; for(int i=0;i<n;i++) ans+=data[i]*data[i]; return ans; } i

delphi调用HttpQueryInfo函数编译报错,说参数类型不符合

问题描述 delphi调用HttpQueryInfo函数编译报错,说参数类型不符合 procedure Get(url,heads: string;res: TStream); var hInt,hUrl:HINTERNET; buffer:PChar; dwRead:cardinal; hBuf:PChar; flag:Boolean; len,index:Integer; var value: DWORD; begin GetMem(buffer, 65536); GetMem(hBuf,

android-新手求助:安卓sql查找的函数,报错Nullpointerexception

问题描述 新手求助:安卓sql查找的函数,报错Nullpointerexception 代码如下 public User search(String username){ SQLiteDatabase sdb = helper.getReadableDatabase(); Cursor cursor =sdb.rawQuery("select * from user where username=?", new String[]{username}); if(cursor.moveTo

java-Java函数substring()报错

问题描述 Java函数substring()报错 解决方案 substring是String类的方法,当然会报编译错误了.你可以用自动提示ctrl+1就能知道错误原因了. 看你是想用字符数组a来创建str,可以直接用: String str = new String(a); 解决方案二: a是字符数组,没有substring这个方法,你是不是想获取s的字串? 解决方案三: a是一个数组,不难这样玩儿,substring属于String. 解决方案四: 1.将光标移到小红叉的地方,能看到错误的描述

hp函数setcookie()报错:Warning: Cannot modify header

快要下班的时候,看到php讨论学习群中有朋友说设置cookie的时候.向他要了代码看了原因!报错 Warning: Cannot modify header information – headers already sent by (output started at cookie1.php:4) in cookie1.php on line 5 <?php ob_start(); setcookie("username","宋岩宾",time()+3600

TO_DATE函数索引报错ORA-01743

开发同学有一个需求,如下这张表: CREATE TABLE TBL_EFFDATE (ROUTID NUMBER(20,0) NOT NULL, EFFDTE CHAR(7), EDDATE CHAR(7), ICID CHAR(1), FREQ CHAR(7) ); 其中EFFDTE保存的是DDMONYY格式的日期,由于表之前未有归档机制,因此产生了许多垃圾数据,现在需要根据EFFDTE删除16年以前的所有数据,表的数据量在百万级,16年以前的占了绝大部分. 对于这需求来说,SQL的条件很明确

android-构造函数intent报错 undefined

问题描述 构造函数intent报错 undefined 使用fragment中有一个下拉列表,我想在选择下拉列表项目时启动一个新activity.结果报错: The constructor Intent(UserHomeActivity Class) is undefined UserHomeActivity.java /SwipeyTabs/src/com/recscores/android line 28 Java Problem 实现代码: public class UserHomeAct

晕呀,为何这么简单的一句话也报错呀Workbook.createWorkbook(os)

问题描述 OutputStreamos=newFileOutputStream("c:\test.xls");//输出的Excel文件URLWritableWorkbookwwb=Workbook.createWorkbook(os);//创建可写工作薄程序执行到Workbook.createWorkbook(os),报错.也没有提示信息.就弹出错误框,报Error.为什么呀.这个错误也很怪异呀. 解决方案 解决方案二:你有没有try?打印出来看看.

vs2012配置opencv后,函数报错,求大神解释

问题描述 vs2012配置opencv后,函数报错,求大神解释 我在配置好opencv2.4.10之后,找了个程序测试一下,但是程序中所有的opencv库中的函数都报错,说是为定义的标示符,但是opencv的库函数我已经链接输入了.搞了好久,求大神解答额 解决方案 opencv的lib库是否也用vs2012编译的,其次,link的时候路径等是否设置争取