• C++内存管理及模板入门详解


    一:内存管理

    1.1:C++内存分布

    image-20221020100441499

    看下面一段代码:

    int globalVar = 1;
    
    static int staticGlobalVar = 1;
    
    void Test()
    
    {
    
    static int staticVar = 1;
    
    int localVar = 1;
    
    int num1[10] = {1, 2, 3, 4};
    
    char char2[] = "abcd";
    
    char* pChar3 = "abcd";
    
    int* ptr1 = (int*)malloc(sizeof (int)*4);
    
    int* ptr2 = (int*)calloc(4, sizeof(int));
    
    int* ptr3 = (int*)realloc(ptr2, sizeof(int)*4);
    
    free (ptr1);
    
    free (ptr3);
    
    }
    
    • 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

    选项: A.栈 B.堆 C.数据段(静态区) D.代码段(常量区)

    globalVar在哪里?__C staticGlobalVar在哪里?__C

    staticVar在哪里?C__ localVar在哪里?A

    num1 在哪里?A__

    char2在哪里?_A *char2在哪里?A

    pChar3在哪里?A *pChar3在哪里?D__

    ptr1在哪里?A *ptr1在哪里?B____

    原因:这里*char2 和 pchar3 易错,char2是一个数组,所以首先会拷贝常量字符串abcd到数组中,前者就是相当于拿到存放在栈中的字符a,后者是指针,指向常量字符串的地址,并没有发生拷贝,所以pchar3是在代码段中拿到了a,所以是在代码段。

    sizeof(num1) = 40__;

    sizeof(char2) = __5; strlen(char2) = 4;

    sizeof(pChar3) = 4____; strlen(pChar3) = 4__;

    sizeof(ptr1) = 4__;

    1.2:new和delete

    c++中使用new和delete进行动态内存管理。

    int main()
    {
    	int* a1 = new int;//开辟一个int类型的空间,并不初始化。
    	int* a2 = new int(10);//开辟一个int类型的空间并且初始化为10。
    	int* a3 = new int[10];//开辟一个int类型的空间大小为10个int。
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    针对上面开辟的空间如何释放?

    	delete a1;
    	delete a2;
    	delete[] a3;
    
    • 1
    • 2
    • 3

    申请和释放单个元素的空间,用new和delete操作数,多个元素的空间用new[]和delete[] 。(一定要匹配使用)

    class test
    {
    public:
    	test(int a = 0)
    		:_a(a)
    	{
    		cout << "构造调用成功" << endl;
    	}
    	~test()
    	{
    		cout << "析构调用成功" << endl;
    	}
    
    private:
    	int _a;
    };
    int main()
    {
    	test* a1 = new test(1);
    	test* a2 = (test*)malloc(sizeof(test));
    	delete a1;
    	free(a2);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    image-20221020102153669

    针对自定义类型new和delete会调用构造和析构函数,而malloc和free不会。

    1.3:operator new和operator delete函数

    这2个不是运算符重载,是系统提供的全局函数。new在底层调用operator new实现,delete在底层调用operator delete实现。

    image-20221020102745350

    image-20221020102757686

    operator new实际上也是通过malloc来实现,申请成功就返回,否则执行用户提供的空间不足应对措施,如果提供该措施,就继续申请空间,如果不提供就会抛异常。

    1.3:malloc free和new delete的区别

    • 前者是函数,后者是操作符
    • 前者申请空间的时候会初始化,后者不会
    • 前者申请空间需要计算空间大小,后者不需要,因为后面跟的是空间类型,如果是多个对象,[]即可
    • 前者返回值为void*需要用户自己进行强转,后者不需要,因为后面跟着空间类型
    • 前者申请空间失败的时候会返回NULL,因此需要判空,后者不需要,但是需要捕获异常
    • 针对自定义类型,前者开辟空间不会调用构造和析构函数,后者会。

    二:模板初阶

    2.1:泛型编程

    前面章节我们学习过函数重载,这里复习一下,形参的顺序或者个数或者类型不同可以构成函数重载。(返回值不同不行)

    int add(int a, int b)
    {
    	return a + b;
    }
    double add(double a,double b)
    {
        return a+b;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    但是每一次形参类型不一样,我们就要再写一个这样的函数,比较麻烦,代码的维护下较低,一个函数出错可能所哟重载都出错。

    我们在铸造汽车的时候,都会使用汽配模具,因此c++是否也能使用模具呢?

    2.2:函数模板

    2.2.1:模板隐式实例化

    typename 可以用class替代,但是不能用struct替代。

    template
    void swap(T& a, T& b)
    {
    	T temp = a;
    	a = b;
    	b = temp;
    }
    int main()
    {
    	int a1 = 2, a2 = 3;
    	double a3 = 4.4, a4 = 4.7;
    	swap(a1, a2);
    	swap(a3, a4);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    image-20221020105016088

    如果是这样呢?

    	swap(a1, a3);
    
    • 1

    会报错;

    image-20221020105101017

    因为swap是个模板函数,T的类型是根据传参的变量类型推演的,a1是int类型,那么编译器就会让T为int类型,但是a3是double,类型不匹配出错。

    2.2.2:模板显式实例化

    T add(T a, T b)
    {
    	return a + b;
    }
    int main()
    {
    	int a1 = 2, a2 = 3;
    	double a3 = 4.4, a4 = 4.7;
    	add(a1, a3);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    可以指定add模板后面的参数类型为<>中的类型,编译器会自己进行隐式类型转换。转换不成功就会报错。

    2.2.3:模板参数的匹配原则

    int add(int a, int b)
    {
    	printf("调用专用函数\n");
    	return a + b;
    }
    template
    T add(T a, T b)
    {
    	printf("调用函数模板\n");
    	return a + b;
    }
    
    int main()
    {
    	add(1, 2);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    我们定义了2个函数,一个是专用于int类型的加法函数,一个是函数模板。

    image-20221020110336432

    运行结果如下,因此我们得出结论。

    对于非模板函数和同名函数模板,如果其他条件都相同, 在调用时候优先调用非模板函数,如果模板可以产生一个更好匹配的函数,那么选择模板。

    模板不允许自动类型转换,手动添加<>,普通函数可以自动类型转换。

    2.3:类模板

    template
    class vector
    {
    public:
    	vector(int a = 0)
    		:_data(new T[_capacity])
    		, _capacity(4)
    	{
    
    	}
    	~vector();//这里放一个声明,看看定义如何定义
    private:
    	T* _data;
    	size_t _capacity;
    
    };
    template
    vector::~vector()
    {
    	delete[] _data;
    	_capacity = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    注意,析构函数在类外定义的时候,需要加上vector这个模具所带的参数列表T。

    2.3.1:类模板实例化

    vector s1;
    
    • 1

    类模板的实例化需要类模板名字后跟<>,然后将实例化的类型放在<>中即可。类实例化的结果才是真正的类。vector< int >才是类名,vector不是真正的类。

  • 相关阅读:
    ETLCloud助力富勒TMS实现物流数仓同步
    如何创建自定义前端组件?
    【Verilog】跨时钟域处理(一)——多bit MUX同步
    HTTP协议深入理解+如何使用Fiddler抓包
    【南外夏令营】线性同余方程
    Idea Java开发必备插件
    【云原生之k8s】kubernetes核心组件
    对wangeditor进行扩展---- 源代码(上)
    网格bfs,LeetCode 2684. 矩阵中移动的最大次数
    iOS 内存管理和优化
  • 原文地址:https://blog.csdn.net/phenix_417/article/details/127423602