C++ delete之静态变量问题详解

 更新时间:2021年09月24日 16:27:02   作者:weixin_43436587  
这篇文章主要为大家详细介绍了C++delete的一些问题,学习如何动态创建对象,动态创建的对象与一般对象的区别,动态创建的对象的初始化以及释放动态分配的内存等知识点,感兴趣的朋友可以参考一下

delete释放的指针,再访问

例1

#include <iostream>
using namespace std;
class Box
{
public:
    Box(int,int);
    ~Box();
    void volume();
    static int height;
    int width;
    int length;
};
Box::Box(int wi, int le)
{
    width = wi;
    length = le;
}
Box::~Box(){cout<<"the pointer is released."<<endl;}
void Box::volume()
{
    cout<<height*width*length<<endl;
}
int Box::height = 100;
int main()
{
    Box* p = new Box(10,20);
    delete p;
    cout<<p->height<<endl;
    cout<<Box::height<<endl;
    cout<<"width" <<p->width<<endl;
    cout<<"length "<<p->length<<endl;
    p->volume();
    return 0;
}

//输出:
/*100
100
width 16257288
length 16253120
-1812113408*/

例2

#include <iostream>
using namespace std;
int * func(){
    int * a = new int(10);
    return a;
}
int main(){
    int * p = func();
    cout << *p << endl;//10
    //delete关键字用来释放堆区数据
    delete p;
//    p = new int(5);
    cout << *p << endl;//10
    return 0;
}

//输出
// 10
// 16584968

解释:

访问 delete 之后的内存是一个未定义行为。 未定义行为可能产生任何结果,包括但不限于:产生期望的结果,产生未期望的结果,产生随机的结果,产生无法解释的结果,运行错误,随机的运行时错误,编译错误,等等 ---- 你只是放弃了对这片内存的所有权。获得所有权的人对这片内存做什么(或者说什么都不做)都不关你的事

static 变量的储存区域

https://blog.csdn.net/qq_32900237/article/details/107094377?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242参考文章

例1

#include <iostream>
using namespace std;
class Box
{
public:
    Box(int,int);
    ~Box();
    void volume();
    static int height;
    int width;
    int length;
};
Box::Box(int wi, int le)
{
    width = wi;
    length = le;
}
Box::~Box(){cout<<"width: "<< width <<"the pointer is released."<<endl;}
void Box::volume()
{
    cout<<height*width*length<<endl;
}
int Box::height = 100;
int main()
{
    Box* p = new Box(10,20);
    cout<<"point  "<<p<<endl;  //point  0xe91470
    cout<<&(p->height)<<endl;  //0x405004
    cout<<&(p->width)<<endl;   //0xe91470
    cout<<&(p->length)<<endl;  //0xe91474
    cout<<sizeof(p)<<endl;    //4
    cout<<sizeof(*p)<<endl;   //8
    cout<<sizeof(Box)<<endl;  //8
	//delete p;              //width: 10the pointer is released.  用new创建的对象,必须自己用delete回收,不然系统不会帮助回收,出现内存泄漏
    Box a = Box(1,2);
    Box *pa = &a;
    cout<<"point  "<<pa<<endl;  //point  0x61ff00
    cout<<&(pa->height)<<endl;  //0x405004
    cout<<&(pa->width)<<endl;   //0x61fefc
    cout<<&(pa->length)<<endl;  //0x61ff00
    cout<<sizeof(pa)<<endl;     //4
    cout<<sizeof(*pa)<<endl;    //8
    cout<<sizeof(a)<<endl;      //8
    Box b = Box(3,4);
    Box *pb = &b;
    cout<<"point  "<<pb<<endl;  //point  0x61fef4
    cout<<&(pb->height)<<endl;  //0x61fef4
    cout<<&(pb->width)<<endl;   //0x61fef4
    cout<<&(pb->length)<<endl;  //0x61fef8
    cout<<sizeof(pb)<<endl;
    cout<<sizeof(*pb)<<endl;
    return 0;
}
/*
point  0xe91470       新对象的地址
0x405004              静态变量和普通变量地址不连续,是静态变量存在数据段
0xe91470              普通变量存在 开辟的堆上
0xe91474
4                    指针大小
8                    对象所占内存大小
8                    类大小
point  0x61fefc      新对象a的地址
0x405004             静态变量地址不变,静态变量属于整个类
0x61fefc             属于局部变量,普通变量存在 栈空间上
0x61ff00
4
8
8
point  0x61fef4     新对象b的地址, b与a之间相差8个字节
0x405004            静态变量地址不变,静态变量属于整个类
0x61fef4            属于局部变量,普通变量存在 栈空间上,地址连续
0x61fef8
4
8
width: 3the pointer is released.   自动调用析构函数
width: 1the pointer is released.   自动调用析构函数
*/

例2 帮助理解

#include <iostream>
using namespace std;
class Box
{
public:
    Box(int,int);
    ~Box();
    void volume();
    static int height;
    int width;
    int length;
};
Box::Box(int wi, int le)
{
    width = wi;
    length = le;
}
Box::~Box(){cout<<"width: "<< width <<"the pointer is released."<<endl;}
void Box::volume()
{
    cout<<height*width*length<<endl;
}
int Box::height = 100;
int main()
{
    Box* p = new Box(10,20);
    cout<<"point  "<<p<<endl;
    cout<<&(p->height)<<endl;
    cout<<&(p->width)<<endl;
    cout<<&(p->length)<<endl;
    cout<<sizeof(p)<<endl;
    cout<<sizeof(*p)<<endl;
    cout<<sizeof(Box)<<endl;
    // delete p;
    Box* p1 = new Box(30,40);
    cout<<"point  "<<p1<<endl;
    cout<<&(p1->height)<<endl;
    cout<<&(p1->width)<<endl;
    cout<<&(p1->length)<<endl;
    cout<<sizeof(p1)<<endl;
    cout<<sizeof(*p1)<<endl;
    cout<<sizeof(Box)<<endl;
    delete p;
    delete p1;
    Box a = Box(1,2);
    Box *pa = &a;
    cout<<"point  "<<pa<<endl;
    cout<<&(pa->height)<<endl;
    cout<<&(pa->width)<<endl;
    cout<<&(pa->length)<<endl;
    cout<<sizeof(pa)<<endl;
    cout<<sizeof(*pa)<<endl;
    cout<<sizeof(a)<<endl;
    Box b = Box(3,4);
    Box *pb = &b;
    cout<<"point  "<<pb<<endl;
    cout<<&(pb->height)<<endl;
    cout<<&(pb->width)<<endl;
    cout<<&(pb->length)<<endl;
    cout<<sizeof(pb)<<endl;
    cout<<sizeof(*pb)<<endl;
    return 0;
}
/*
point  0x791470
0x405004       
0x791470       
0x791474       
4
8
8
point  0x791108
0x405004       
0x791108       
0x79110c       
4
8
8
width: 10the pointer is released.
width: 30the pointer is released.
point  0x61fef8
0x405004
0x61fef8
0x61fefc
4
8
8
point  0x61fef0
0x405004
0x61fef0
0x61fef4
4
8
width: 3the pointer is released.
width: 1the pointer is released.
*/

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

  • C语言中0数组\柔性数组的使用详解

    C语言中0数组\柔性数组的使用详解

    这篇文章主要给大家介绍了关于C语言中0数组\柔性数组使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • C++无try-catch的异常捕获示例详解

    C++无try-catch的异常捕获示例详解

    这篇文章主要为大家介绍了C++无try-catch的异常捕获示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • C语言修炼之路灵根孕育源流出 初识C言大道生上篇

    C语言修炼之路灵根孕育源流出 初识C言大道生上篇

    C语言是一门面向过程、抽象化的通用程序设计语言,广泛应用于底层开发。C语言能以简易的方式编译、处理低级存储器。C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言
    2022-03-03
  • C++实现LeetCode(87.搅乱字符串)

    C++实现LeetCode(87.搅乱字符串)

    这篇文章主要介绍了C++实现LeetCode(87.搅乱字符串),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • 关于Visual Studio无法打开源文件

    关于Visual Studio无法打开源文件"stdio.h"问题

    这篇文章主要介绍了关于Visual Studio无法打开源文件"stdio.h"问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 一文详解C语言char类型中的存储

    一文详解C语言char类型中的存储

    C语言中的char是用于声明单个字符的关键字,这篇文章主要给大家介绍了关于C语言char类型中存储的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • C++实现LeetCode(207.课程清单)

    C++实现LeetCode(207.课程清单)

    这篇文章主要介绍了C++实现LeetCode(207.课程清单),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C++中关于多态实现和使用方法

    C++中关于多态实现和使用方法

    这篇文章主要介绍了C++中关于多态实现和使用方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 深入uCOS中全局变量的使用详解

    深入uCOS中全局变量的使用详解

    本篇文章是对uCOS中全局变量的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C++遍历文件夹下文件的方法

    C++遍历文件夹下文件的方法

    这篇文章主要介绍了C++遍历文件夹下文件的方法,实例分析了C++针对文件夹遍历的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07

最新评论