7302人加入学习
(18人评价)
C++编程系列 第一季编程基础

制作于2018年2月7日

价格 免费

int * p = new int;

*p = 100;

声明一个新的整数类型区域 赋值

delete p;

new出来必须通过delete释放

[展开全文]

 

#include <iostream>

using namespace std;

int main()

{

//int a = 10;

//float b = 9.7f;

//int c = 20;

 

// & 取得一个变量的内存地址

//cout << &a << endl; // 0093F748

//cout << &b << endl; // 0093F73C

// * 从内存地址所对应的内存处 取得数据

//cout << *(&a) << endl; // 10

//cout << a << endl; // 10

// error: cout << *a << endl;

 

//int* pa = &a;

//float* pb = &b;

 

//cout << pa << endl; // 0079F722

//cout << pb << endl; // 0079F71C

 

//cout << *pa << endl; // 10

//cout << *pb << endl; // 9.7

 

//int* p;

//p = pa;

//cout << *p << endl; // 10

//cout << *pa << endl; // 10

//*pa = 100;

//cout << a << " " << *pa << endl; // 100 100

//*p = 300;

//cout << a << " " << *pa << " " << *p << endl; // 300 300 300

 

//int* p1 = NULL;

//int* p2 = 0;

//int* p3 = nullptr; // C++11

//cout << p1 << " " << p2 << " " << p3 << endl;;// 00000000 00000000 00000000

 

// 笔记:void 返回类型/任意类型指针

//void* p; // p 可以接收任意类型的指针

//p = &a;

////p = &b;

//cout << *((int*)p)<< endl; // 10

 

//int* p = new int;

//cout << *p << endl; // -842150451,没初始值

 

int* p = new int;

*p = 100;

cout << *p << endl; // 100

delete p; // 释放内存空间,通过new申请的,需要用delete释放

 

return 0;

}

[展开全文]
  1. new出来的空间要通过delete释放 要不然会一直被占用
[展开全文]