-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewDeleteTest.cpp
More file actions
40 lines (34 loc) · 1.08 KB
/
NewDeleteTest.cpp
File metadata and controls
40 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// NewDeleteTest.cpp
// algoritm
//
// Created by wizardholy on 2016/12/25.
// Copyright © 2016年 wizardholy. All rights reserved.
//
/*
我们应该使用C++语言提供的new与new[]操作符,而不要使用C语言提供的malloc函数。
c使用malloc 和 free 进行内存分配
虽然malloc函数具有分配存储空间的功能,但是这些函数除了分配存储空间外,不会调用类的构造函数。
而C++语言提供的new和new[]操作符则不会如此,使用它们为对象分配存储空间的同时,它们也会调用相应的构造函数。
操作符delete和delete[]在释放对象存储空间的同时也会调用析构函数,而free函数则不会调用析构函数。
*/
#include<iostream>
using namespace std;
class test
{
public:
test(int i = 1){num = i;cout<<num<<" Constructor"<<endl;}
~test(){cout<<num<<" Destructor"<<endl;}
private:
int num;
};
int main()
{
test * t0 = new test(0);
test * t1 = new test[5];
test * t2 = (test *)malloc(sizeof(test));
delete t0;
delete[] t1;
free(t2);
return 0;
}