以为这样的变量会再函数被运行结束后由系统释放
#include <iostream>
using namespace std;
int GetLength(char *x1)
{
char * temp = x1;
int i = 0;
while(*temp)
{
++i;
++temp;
}
return i;
}
void addzezo(char * &x2,int count)
{
int i(0);
char * temp = new char[20];
for(i = 0;i<count;++i)
{
temp[i] = '0';
}
temp[i] = '\0';
x2 = strcat(temp,x2);
}
char * plus(char *x1,char *x2)
{
int length(0);
int count = abs(GetLength(x1)-GetLength(x2));
if(GetLength(x1)>GetLength(x2))//in this case the x2 should add 0
{
addzezo(x2,count);
cout<<x2<<endl;;
length = GetLength(x1);
}
else
{
addzezo(x1,count);
cout<<"x1 = "<<x1<<endl;
length = GetLength(x2);
}
//ready end
//begin to caculate
int result[11] = {0};
for(int i = length-1;i>-1;--i)
{
result[length-1-i] = (x1[i]-'0')+(x2[i]-'0');
}
//deal with the result arrary
int flag;
for(int i =0;i<length;++i)
{
result[i+1] =result[i+1]+result[i]/10;//advanced numbers add to the advanced poisition
result[i]=result[i]%10;//the left left
flag=i;
}
//get the char result
char * ch_result = new char[20];
if(result[flag+1]!=0)//in this case there is an advanced in the highest bit the length++
for(int i = 0;i<=length;++i)
{
ch_result[i] = result[i]+'0';
}
else
for(int i = 0;i<length;++i)
{
ch_result[i] = result[i]+'0';
}
//ch_result[i] = '\0';
return ch_result;
}
int main()
{
cout<<
plus("123456","22211111111")<<endl;
int x1 = 2;
cout<<x1<<endl;
cout<<x1;
system("pause");
return 0;
}
其它的体会:
1、对于已经释放的变量 这个变量会产生新的地址但是 这个地址上不能运行 realloc函数等等
2、要想进行局部变量的返回,需要在heap区进行 进行 new 但是 如何释放他们是个问题