运行结果:
- aa is constructed.
- aa is completed.
- 4984904
函数内部的申请空间要及时释放,否则容易造成内存重复申请和内存迷失。
- //============================================================================
- // Name : main.cpp
- // Author : ShiGuang
- // Version :
- // Copyright : sg131971@qq.com
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <string>
- using namespace std;
- class aa
- {
- public:
- aa(int a = 1)
- {
- cout << "aa is constructed." << endl;
- id = a;
- }
- ~aa()
- {
- cout << "aa is completed." << endl;
- }
- int id;
- };
- void p()
- {
- aa *s = new aa[9999];
- }
- int main(int argc, char **argv)
- {
- for (;;)
- p();
- return 0;
- }
