Posted on 2008-12-01 01:00
藍(lán)塵 閱讀(148)
評(píng)論(0) 編輯 收藏 引用
1.常規(guī)方法
示例:
void userCode(const char* s1, const* s2)
{
char* copy = new char[strlen(s1)+1];
strcpy(copy,s1);
//用try塊來防止內(nèi)存泄露
try{
//
.
delete[] copy;
char *copy = new char[strlen(s1)+strlen(s2)+1];
strcat(copy,s1);
strcat(copy,s2);
//
}catch(
){
delete[] copy;
throw;
}
delete[] copy;
}
2.使用標(biāo)準(zhǔn)化的字符串類
#include<string>
void userCode(const std::string& s1, const std::string& s2)
{
std::string copy = s1;
//
copy += s2;
//
}
std::string實(shí)現(xiàn)了內(nèi)存管理
//哪天查看下string的實(shí)現(xiàn)