65,189
社区成员




Bank::Bank(char * name,char * account,deposit amount)
{
int x = strlen(name);
int y = strlen(account);
Customer_Name = new char[x+1];
Account = new char[y+1];
strcpy(Customer_Name,name);
strcpy(Account,account);
//strncpy(Customer_Name,name,x);
//strncpy(Account,account,y);
//Customer_Name[x+1] = '\0';
//Account[y+1] = '\0';
Amount = amount;
}
Bank::~Bank()
{
delete [] Customer_Name;
delete [] Account;
}
Customer_Name[x+1] = '\0';
这个相当于赋值到x+2的位置了。。。
#include "iostream"
using namespace std;
void main()
{
char *name="戏++学编程";
char *account="www.vs2003.net";
int x = strlen(name);
int y = strlen(account);
char* Customer_Name = new char[x+1];
char* Account = new char[y+1];
/*strcpy(Customer_Name,name);
strcpy(Account,account);*/
strncpy(Customer_Name,name,x);
strncpy(Account,account,y);
Customer_Name[x] = '\0';
Account[y] = '\0';
printf("%s:%s\n",Customer_Name,Account);
system("pause");
}
离成功其实就是差那么一点,多加了个1 ,越界了