#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請100個char類型的內(nèi)存
char *str =new char(100);
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout << str << endl;
//釋放內(nèi)存
delete []str;
str = NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請100個char類型的內(nèi)存
char *str =new char(100);
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout << str << endl;
//釋放內(nèi)存
delete []str;
str = NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
int x = 3;
//定義引用,y是x的引用
int &y = x;
//打印x和y的值
cout << x << endl;
//修改y的值
y = 4;
//再次打印x和y的值
cout << x << endl;
return 0;
}
using namespace std;
int main(void)
{
int x = 3;
//定義引用,y是x的引用
int &y = x;
//打印x和y的值
cout << x << endl;
//修改y的值
y = 4;
//再次打印x和y的值
cout << x << endl;
return 0;
}
#include <string.h>
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請100個char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout<<str<<endl;
//釋放內(nèi)存
delete []str;
str=NULL;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
//在堆中申請100個char類型的內(nèi)存
char *str = new char[100];
//拷貝Hello C++字符串到分配的堆中的內(nèi)存中
strcpy(str, "Hello imooc");
//打印字符串
cout<<str<<endl;
//釋放內(nèi)存
delete []str;
str=NULL;
return 0;
}
5-3練習(xí),題目有問題。D中講得不時說要用,delete []p 嗎?我自己在電腦上敲了下,int *p=new int (20) delete p 同樣是對的.
2015-09-03
new <--> delete
malloc<---> free
配對使用,申請后,檢查是否成功。
malloc<---> free
配對使用,申請后,檢查是否成功。
2015-08-30