首页 新闻 赞助 找找看

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

0
悬赏园豆:20 [已解决问题] 解决于 2015-04-04 00:33

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

}

 

不用内存检测工具是运行是正确的,但是用valgrind运行报错,内容:

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

报告中说没有内存泄漏:All heap blocks were freed -- no leaks are possible,我想其中关键的应该是这一句:Address 0x5a12070 is 0 bytes after a block of size 48 alloc'd,但不知道是什么意思?难道分配内存失败吗,那怎么会正常的运行并得到正确的结果呢?真让人迷惑,哪位高手能指教一下?

Patrickz10的主页 Patrickz10 | 初学一级 | 园豆:7
提问于:2015-04-02 23:43
< >
分享
最佳答案
0

没细分析你的代码和结果,但中间一行代码:

wchar_t *ws2 = (wchar_t)malloc(len*sizeof(wchar_t));

应该是有问题,正确的应该是:

wchar_t *ws2 = (wchar_t*)malloc(len*sizeof(wchar_t));

对不?尝试下。

收获园豆:20
519740105 | 大侠五级 |园豆:5810 | 2015-04-03 09:07

我先在CSDN提问的,那个网站真奇怪,把我的尖括号全部去掉了,信号*也选择性地去掉几个,我没有注意到就贴过来,所以你说的语法问题,本来代码是好的

Patrickz10 | 园豆:7 (初学一级) | 2015-04-03 11:18

但是我现在知道问题在哪里了,就这一句wstring wstr(ws2);可能不合规范吧?

Patrickz10 | 园豆:7 (初学一级) | 2015-04-03 11:19

@Patrickz10: 看下wstring类的构造定义。

519740105 | 园豆:5810 (大侠五级) | 2015-04-03 11:27

@Patrickz10: 另:

wstring wstr(ws2);
free(ws2);

你构建了wstr,然后返回,但是,在返回前,你free了ws2。

如果wstring的构造只是简单的使用ws2的引用,那么问题就在这里了,如果是把内容另行复制了,free是不会有影响的。

519740105 | 园豆:5810 (大侠五级) | 2015-04-03 11:28

@519740105: 是啊,我记得string好像是复制,但是这里也许wstring就是使用了它的引用,难怪我注释掉构造那一行就没有错误了,不过我后来换了一个方式处理这个问题,不再去转换字符,所以也就没有心思去追究原因了,还是你看出来提醒了我,要不还是留一个疙瘩

Patrickz10 | 园豆:7 (初学一级) | 2015-04-04 00:31

@Patrickz10: 补充:上面说的不是完全注释掉,而是注释掉构造宽字符串的那个参数

Patrickz10 | 园豆:7 (初学一级) | 2015-04-04 00:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册