在c ++中strdup [英] strdup in c++
问题描述
您好,
我有一个用c ++编写的程序,我希望使用类似的函数来支持
strdup()。
我遇到问题的原因是char数组需要释放以避免内存泄漏,但是使用delete []会出现问题;
例如
char * NewCharArray;
NewCharArray = strdup(" Some text");
delete [] NewCharArray;
Nybody有什么想法吗?
谢谢
Allan
Hi there,
I have a program written in c++ and I wish to use a similar function to
strdup().
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];
e.g.
char *NewCharArray;
NewCharArray = strdup("Some text");
delete []NewCharArray;
Nybody have any ideas?
Thanks
Allan
推荐答案
" Allan Bruce" <人***** @ TAKEAWAYf2s.com>在消息新闻中写道:bk ********** @ news.freedom2surf.net ...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:bk**********@news.freedom2surf.net...
你好,
我有一个用c ++和我希望使用类似的功能
strdup()。
我遇到问题的原因是char数组需要释放以避免内存泄漏,但是我在使用delete时遇到问题[];
例如
char * NewCharArray;
NewCharArray = strdup(" Some text");
> delete [] NewCharArray;
Nybody有什么想法?
Hi there,
I have a program written in c++ and I wish to use a similar function to
strdup().
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];
e.g.
char *NewCharArray;
NewCharArray = strdup("Some text");
delete []NewCharArray;
Nybody have any ideas?
std :: string NewString;
NewString =" SomeText";
//删除不需要。
std::string NewString;
NewString = "SomeText";
// delete not required.
Allan Bruce写道:
Allan Bruce wrote:
你好,
我有一个用c ++编写的程序,我希望使用类似的功能
strdup()。
我遇到问题的原因是char数组需要释放以避免内存泄漏,但我使用delete [];
例如
char * NewCharArray;
NewCharArray = strdup(" Some text");
delete [] NewCharArray;
Nybody有什么想法?
Hi there,
I have a program written in c++ and I wish to use a similar function
to strdup().
The reason I have problems is that the char array requires freeing to
avoid a memory leak, but I get problems using delete[];
e.g.
char *NewCharArray;
NewCharArray = strdup("Some text");
delete []NewCharArray;
Nybody have any ideas?
Nybody一定是斯堪的纳维亚人:-)
使用std :: string而不是摆弄那些数组。
-
Attila aka WW
Nybody must be some Scandinavian guy :-)
Use std::string instead of fiddling with those arrays.
--
Attila aka WW
>
" Allan Bruce" <人***** @ TAKEAWAYf2s.com>写了
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote
我有一个用c ++编写的程序,我希望使用类似的功能
strdup()。
我遇到问题的原因是char数组需要释放以避免内存泄漏,但是我在使用delete []时遇到问题;
I have a program written in c++ and I wish to use a similar function to
strdup().
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];
你必须免费使用:
#include< cstdlib>
#include< cstring>
// ...
char * NewCharArray = strdup(你好);
// std :: strdup在我的机器上无法编译
//(宏?)。干得好我从不使用它。
std :: free(NewCharArray);
但不要这样做。使用标准字符串类。
问候,
巴斯特
You have to use free:
#include <cstdlib>
#include <cstring>
// ...
char * NewCharArray = strdup ("Hi there");
// std::strdup doesn''t compile on my machine
// (macro?). Good job I never use it.
std::free (NewCharArray);
But don''t do that. Use the standard string class.
Regards,
Buster
这篇关于在c ++中strdup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!