C 中char数组 C++可使用string2.c与c++ struct
C中 命名为 struct className varName; C++ 可省略struct 直接 className varName;3.指针delete的使用
- delete只能用于释放new分配的内存 - new [] 为数组分配,需使用delete[] 去释放。 - new [] 为实体分配,需使用delete去释放。
int *a = new int;
delete a ;// OK
delete a ;//not ok 不要尝试释放 释放过的指针,会引起不可预知的错误。
int b = 5;
int *c = &b;
delete c;//not ok, delete只适用于释放new 分配的内存
4.c++结构体指针跟非指针的访问
int main()
{
using namespace std;
struct things {
int good;
int bad;
};
things grubnose = {3,400};
things * pt = &grubnose;
cout << grubnose.bad << ";" << grubnose.good << endl;
cout << pt->bad << ";" << pt->good << endl;
cout << "-----------" << endl;
grubnose.good = 100;
cout << grubnose.bad << ";" << grubnose.good << endl;
cout << pt->bad << ";" << pt->good << endl;
cout << "-----------" << endl;
pt->bad = 500;
cout << grubnose.bad << ";" << grubnose.good << endl;
cout << pt->bad << ";" << pt->good << endl;
cout << (*pt).bad << ";" << (*pt).good << endl;
cout << "-----------" << endl;
return 0;
}
结果:
400;3 400;3 ----------- 400;100 400;100 ----------- 500;100 500;100 500;100 -----------5.C++分配内存的方法
- 自动存储:函数内部成员变量,函数执行完毕自动释放。
- 静态存储:函数外定义,或使用static定义,运行过程中全程占用内存。
- 动态存储:new delete 运算符,new 需对应一个delete,否则易导致内存泄漏。
- 线程存储:thread_local 修饰 线程结束时被销毁



