传递char *作为引用并为指针重新分配内存

编程入门 行业动态 更新时间:2024-10-27 01:27:22
本文介绍了传递char *作为引用并为指针重新分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

基本上,测试函数得到一个char指针,并为它分配一个字符串 。然后字符串通过引用调用 机制传回。在test()中,我为指针重新分配了一些内存, 大小不固定。 我记得,所有新语句都应该跟着一个删除声明, 这里有一些内存泄漏吗? void test(char *& str) { //不知道字符串可能有多长 char * astring ="思考后返回的字符串,而不是固定长度" ;; str =新字符[strlen(astring)+1]; strcpy(str,astring); cout<<" test"<< str<< endl; } void main(void) { char * astr ="" ;; test(astr); cout<< ; astr<< endl; }

Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here? void test(char* &str) { // don''t know how long the string might be char *astring = "a string to return after thinking, not fixed length"; str = new char[strlen(astring)+1]; strcpy(str, astring ); cout<<"test "<<str<<endl; } void main(void) { char *astr = ""; test(astr); cout<<astr<<endl; }

推荐答案

happyvalley写道: happyvalley wrote: 基本上,测试函数得到一个char指针,并分配一个stri对它来说是。然后字符串通过引用调用 机制传回。在test()中,我为指针重新分配了一些内存, 大小不固定。 我记得,所有新语句都应该跟着一个删除声明, 这里有一些内存泄漏吗? 无效测试(char *& str) { //不知道字符串可能有多长时间 char * astring ="思考后返回的字符串,而不是 固定长度" ;; str =新字符[strlen(astring)+1]; strcpy(str,astring); cout<<"" test"<< str<< endl; } 无效main(无效) { char * astr ="" ;; test(astr); cout<< astr<< endl; } Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here? void test(char* &str) { // don''t know how long the string might be char *astring = "a string to return after thinking, not fixed length"; str = new char[strlen(astring)+1]; strcpy(str, astring ); cout<<"test "<<str<<endl; } void main(void) { char *astr = ""; test(astr); cout<<astr<<endl; }

是的,这就是泄密。字符串类有什么问题?它有一个成员 函数c_str()返回一个const ptr给char。 #include< iostream> #include< ostream> #include< string> void test(std :: string& r_s) { r_s + ="思考后返回的字符串" ;; } int main() { std :: string s; test(s); std :: cout<< s<< std :: endl; } / * a字符串在思考后返回 * /

Yes, thats a leak. Whats wrong with the string class? It has a member function c_str() which returns a const ptr to char. #include <iostream> #include <ostream> #include <string> void test(std::string& r_s) { r_s += "a string to return after thinking"; } int main() { std::string s; test(s); std::cout << s << std::endl; } /* a string to return after thinking */

Salt_Peter写道: Salt_Peter wrote: happyvalley写道: happyvalley wrote: 基本上,测试函数得到一个char指针,并为其分配一个字符串 。然后字符串通过引用调用 机制传回。在test()中,我为指针重新分配了一些内存, 大小不固定。 我记得,所有新语句都应该跟着一个删除声明, 这里有一些内存泄漏吗? void test(char *& str) { //不知道字符串可能有多长 char * astring ="思考后返回的字符串,而不是固定长度" ;; str =新字符[strlen(astring)+1]; strcpy(str,astring); cout<<" test"<< str<< endl; } void main(void) { char * astr ="" ;; test(astr); cout<< ; astr<<< endl; } Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here? void test(char* &str) { // don''t know how long the string might be char *astring = "a string to return after thinking, not fixed length"; str = new char[strlen(astring)+1]; strcpy(str, astring ); cout<<"test "<<str<<endl; } void main(void) { char *astr = ""; test(astr); cout<<astr<<endl; }

是的,这就是泄密。字符串类有什么问题?它有一个成员 函数c_str()返回一个const ptr给char。 #include< iostream> #include< ostream> #include< string> void test(std :: string& r_s) { r_s + ="思考后返回的字符串" ;; } int main() { std :: string s; test(s); std :: cout<< s<< std :: endl; } / * a字符串在思考后返回 * /

Yes, thats a leak. Whats wrong with the string class? It has a member function c_str() which returns a const ptr to char. #include <iostream> #include <ostream> #include <string> void test(std::string& r_s) { r_s += "a string to return after thinking"; } int main() { std::string s; test(s); std::cout << s << std::endl; } /* a string to return after thinking */

感谢您的回复,是字符串工作,只是想让它成为直接。 main()传递一个指向test(),而新语句 只为指针分配一些内存,而不是创建一个新的指针, 吧。在返回main()之后,指针将在走出 范围时死亡。对吗? 我把删除astr放在main()的末尾,没有错误。 如何判断是否有内存泄漏? 再次感谢

thanks for your reply, yes string works, just want to make it straight. the main() passes a pointer to the test(), while the new statement only allocate some memory for the pointer, not create a new pointer, right. after return to the main(), the pointer will die when go out of scope. right? I put delete astr at the end of main(), no error. how can I check if there is a memory leak? thanks again

> > void main(void) { char * astr ="" ;; test(astr); cout<< astr<< endl; } void main(void) { char *astr = ""; test(astr); cout<<astr<<endl; }

我看到当调用函数测试指针通过 值传递时,你只是传递指针所指向的地址 并且函数中指针的任何更改都不会反映在外部 因此它是内存泄漏... 希望它有帮助......

I see that when the function test is called the pointer is passed by value, you are just passing the address that the pointer is pointing to and any change of the pointer in the function will not reflect outside and hence it is a memory leak... Hope it helps...

更多推荐

传递char *作为引用并为指针重新分配内存

本文发布于:2023-07-29 08:41:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并为   指针   分配   内存   char

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!