confused:vector< char *>和malloc [英] confused: vector<char*> and malloc
问题描述
矢量< char *之> m_Text;
m_Text.resize(1);
char * foo =" FOO";
char * bar =" BAR" ;
char * foobar =(char *)malloc(strlen(foo)+ strlen(bar)+ 1);
if(foobar)
{
strcpy(foobar,foo);
strcat(foobar,bar);
}
m_Text [0] = foobar;
//当m_Text超出范围时,m_Text [0]会被释放吗?如果没有,我应该在析构函数中使用
免费调用(m_Text [0])吗?
Richard写道:vector< char *> m_Text;
m_Text.resize(1);
这使''m_Text''长1个元素。由于''m_Text''在此之前是空的
,它会向它添加1个指针并使其为空。
char * foo =" FOO";
char * bar =" BAR" ;;
char * foobar =(char *)malloc(strlen(foo)+ strlen(bar)+ 1);
if(foobar)
{
strcpy(foobar,foo);
strcat(foobar,bar);
}
此时''foobar''是一个指向7个字符数组的指针,分配在
免费商店中。数组有F,O,O,O,B,A,R,\\0 '
依旧。
m_Text [0] = foobar;
这使得向量''m_Text''中唯一元素的值与指向该7字符数组的指针相同。
。
//当m_Text超出范围时,m_Text [0]会被释放吗?
No.
如果没有,我应该在析构函数中免费调用(m_Text [0])吗?>
可能。假设你在讨论
中类的析构函数,''m_Text''是数据成员。
V
Richard写道:vector< char *> m_Text;
m_Text.resize(1);
char * foo =" FOO";
char * bar =" BAR" ;;
char * foobar =(char * )malloc(strlen(foo)+ strlen(bar)+ 1);
malloc是C,为什么不使用新的?
if(foobar)
{
strcpy(foobar,foo);
strcat(foobar,bar);
}
m_Text [0] = foobar;
//当m_Text超出范围时,m_Text [0]会被释放?如果没有,我应该在析构函数中调用free(m_Text [0])吗?
尝试这个,如果你不想自己管理内存
vector< string> m_Text;
char * foo =" FOO";
char * bar =" BAR";
string foobar(foo);
foobar.append(bar);
m_Text.push_back(foobar);
"凯尔" <在***** @ e.mail.com>在消息中写道
news:dd ********** @ nemesis.news.tpi.pl ...Richard写道:vector< char *> m_Text;
m_Text.resize(1);
char * foo =" FOO";
char * bar =" BAR" ;;
char * foobar =(char * )malloc(strlen(foo)+ strlen(bar)+ 1);
malloc是C,为什么不使用new?
malloc也是C ++ 。
它用于在C ++中分配原始内存。由于new(和new [])分配
空间并构造对象,new(和new [])不适合当
不足以获得信息时构建对象。
尝试这个,如果你不想自己管理内存
vector< string> m_Text;
很棒的建议!
阿里
vector<char*> m_Text;
m_Text.resize(1);
char* foo = "FOO";
char* bar = "BAR";
char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1);
if (foobar)
{
strcpy(foobar, foo);
strcat(foobar, bar);
}
m_Text[0] = foobar;
// Will m_Text[0] get freed when m_Text goes out of scope? If not, should I
call free(m_Text[0]) in the destructor?
Richard wrote:vector<char*> m_Text;
m_Text.resize(1);
This makes ''m_Text'' to be 1 element long. And since ''m_Text'' was empty
prior to that, it adds 1 pointer to it and makes it null.
char* foo = "FOO";
char* bar = "BAR";
char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1);
if (foobar)
{
strcpy(foobar, foo);
strcat(foobar, bar);
}
At this point ''foobar'' is a pointer to 7 character array, allocated in
the free store. The array has ''F'', ''O'', ''O'', ''O'', ''B'', ''A'', ''R'', ''\0''
in it, in sequence.
m_Text[0] = foobar;
This makes the value of the only element in the vector ''m_Text'' to be the
same as the pointer to that 7-character array.
// Will m_Text[0] get freed when m_Text goes out of scope?
No.
If not, should I
call free(m_Text[0]) in the destructor?
Probably. Assuming you''re talking about the destructor of the class in
which ''m_Text'' is a data member.
V
Richard wrote:vector<char*> m_Text;
m_Text.resize(1);
char* foo = "FOO";
char* bar = "BAR";
char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1);
malloc is C, why dont you use new ?
if (foobar)
{
strcpy(foobar, foo);
strcat(foobar, bar);
}
m_Text[0] = foobar;
// Will m_Text[0] get freed when m_Text goes out of scope? If not, should I
call free(m_Text[0]) in the destructor?
try this if you dont want to manage memory on your own
vector<string> m_Text;
char* foo = "FOO";
char* bar = "BAR";
string foobar(foo);
foobar.append( bar );
m_Text.push_back( foobar );
"Kyle" <in*****@e.mail.com> wrote in message
news:dd**********@nemesis.news.tpi.pl...Richard wrote:vector<char*> m_Text;
m_Text.resize(1);
char* foo = "FOO";
char* bar = "BAR";
char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1);
malloc is C, why dont you use new ?
malloc is C++ too.
It is used to allocate raw memory in C++. Since new (and new[]) allocates
space and constructs object(s), new (and new[]) is not suitable when there
is not enough information to construct the object(s).
try this if you dont want to manage memory on your own
vector<string> m_Text;
Great advice!
Ali
这篇关于confused:vector< char *>和malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!