zoukankan      html  css  js  c++  java
  • c++管理内存的几种方式

    #include <iostream>
    #include <memory>
    
    static void versionOne();
    static void versionTwo();
    
    using namespace std;
    
    int main(void) {
    
    	versionOne();
    	versionTwo();
    	versionThree();
    	return EXIT_SUCCESS;
    }
    void versionOne() {
    	//c语言方式开辟内存
    	int *ageC = (int*) malloc(sizeof(int));
    	if(argC) {
    		free(ageC);
    	}
    	char *c = (char*) malloc(100);
    	free(c);
    
    	//c++ 开辟内存方式
    	int *age = new int(25);
    	int *height = new int(160);
    	//缺陷,容易忘掉free 或 delete
    }
    
    void versionTow() {
    	//智能指针
    	std::shared_ptr<int> age(new int(28));
    	std::shared_ptr<int> height(new int(160));
    
    	std::cout << "VersionTwo: you age is " << *age << ", and your height is "
    		<< *height << std::endl;
    	//不需要做任何事情, 内存会自动的释放掉
    	//基本上,不会造成内存泄露问题
    }
    
  • 相关阅读:
    二人组
    对于软件工程的理解
    shell 远程链接
    shell变量
    shell教程
    正则表达式--练习
    git--版本库
    git-版本回退
    git--时光穿梭
    git安装
  • 原文地址:https://www.cnblogs.com/lyxf/p/12358238.html
Copyright © 2011-2022 走看看