Heap head address and heap pointer

Source: Internet
Author: User

 1 #include "stdafx.h" 2 #include "iostream" 3  4 using namespace std; 5  6 class CStu 7 { 8 private: 9     int M_Id;10     int M_Age;11 public:12      void set(int x)13      {14          M_Id = x;15      }16 17 };18 19 int main(int argc, char* argv[])20 {21     CStu *p = new CStu;        //在堆中创建一块可以存储CStu类的内存空间,并用指针p指向这块内存空间的首地址.22                                //因为创建的这个堆的类型是CStu型.所以指针的类型也是CStu型.23     cout << p << endl;         //打印出的是这个申请的堆的首地址.这个地址每次编译后都不一样的.24     cout << &p << endl;        //打印了存放堆地址的这个内存的单元编号.25 26 //---把这个堆的指针消除掉----27     delete p;28     cout << p << endl;        //删除这个指向堆的指针以后,每次运行,p里面的值得都会改变,有不确定性.29                             //所以说在这样的情况下,这个p绝对是个迷途指针.30                             //如果还想找回刚才那个堆的首地址,那是不可能的事,所以一定要注意什么时候用delete.31 32     cout << &p << endl;        //p还是存在的,并且p的地址一直没改变,只是p里面的内容再不断改变而已.33 34     return 0;35 }

 

Heap head address and heap pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.