四大区 | 特点 | |
代码区 | 代码编译前 | 代码区是共享的、只读的 |
全局区 | 全局变量,静态变量(static+普通变量),常量(字符常量,全局常量)都在全局区,由机器系统管理他们的生命周期。 | |
栈区 | 代码编译后 | 局部变量(函数体中或复合语句中)和形参存在栈区。生命周期由编译器管理。不能返回局部变量的地址 |
堆区 | 程序员在堆区开辟内存空间,生命周期由程序员决定。new关键字开辟。 |
1、每次编译时都是用代码区同一段代码。代码区不可修改。
2、全局变量:在函数体外定义的变量。字符常量要注意不是将字符串复制给一个变量后的,将"字符串"一直当成整体直接使用。全局常量:const修饰的全局变量。
3、对于栈区的变量,在执行后立即由编译器释放内存空间,所以不能返回局部变量的地址。
#include<iostream>
using namespace std;
#include<string>
int *fun()
{
int a = 10;
int* p = &a;
return p;
}
int main()
{
int * a=fun();
cout << *a << endl;
cout << *a << endl;
system("pause");
return 0;
}
第二次输出就乱码了,主要原因是因为释放了内存,但是通过指针去访问了不存在的地址int * a=fun();。因为编译器在执行int * a=fun();命令后会先为你保留一次空间,但是第二次就释放空间了,就不能通过该地址访问变量的值了。但是如果返回的是变量而不是地址,依旧可以反复输出,那是因为主函数中定义了局部变量接受该值,该值就存在新开辟的栈区,在主函数中能一直输出。释放内存只是通知系统这块内存我不用了,在未被新对象占用之前,里面的值还是不变的。不能通过该地址去访问该地址的内容,但是为p开辟的内存空间存放的地址是里面的内容是可以输出的。地址和变量值已经断开联系了。
如下情况可以输出
#include<iostream>
using namespace std;
#include<string>
int* fun()
{
int a = 10;
int* p = &a;
return p;
}
int main()
{
cout << *fun() << endl;
cout << *fun() << endl;
}
可是像刚刚这么说,如下情况不是也不能输出吗,可是他输出了
#include<iostream>
using namespace std;
#include<string>
void fun()
{
int a = 10;
int* p = &a;
cout << *p << endl;
cout << *p << endl;
}
int main()
{
fun();
}
解答:???准确的说应该是除main函数体类在其它函数体的非堆区变量,非静态变量需要在函数体执行完毕后编译器才会释放内存。
并且以下局部变量可以输出的,不都是局部变量吗,所以现在对这部分还不是很清晰。
#include<iostream>
using namespace std;
#include<string>
int main()
{
int a = 10;
int* p = &a;
cout << *p << endl;
}
解答:虽然main函数中的变量也是局部变量,但是它的生命周期和全局变量一样长。
4、堆区
new关键字的使用语法(new返回的是改数据类型的指针)
(1)int * p=new int(10);//为10在堆区开辟了一段内存空间
#include<iostream>
using namespace std;
#include<string>
int * fun()
{
int* p = new int(10);
return p;
}
void test()
{
int *p=fun();
cout << *p << endl;
cout << *p << endl;
}
int main()
{
test();
}
类似刚刚栈区的返回,可是由于变量在new开辟的空间,所以释放由程序员决定,所以可以可以通过返回地址访问。程序全部结束后结束。
如何提前结束,借助delete。
delete(p);将地址释放干净。
前面正常输出,但后面未输出,程序报错
验证释放内存在下一个对象未来之前之前的内容还在。下面代码正常输出。
#include<iostream>
using namespace std;
#include<string>
int * fun()
{
int* p = new int(10);
return p;
}
void test()
{
int *p=fun();
cout << *p << endl;
cout << *p << endl;
delete p;
cout << p << endl;
cout << p << endl;
}
int main()
{
test();
}
(2)用new 为size为10的数组在堆区开辟内存空间:new int [10];
#include<iostream>
using namespace std;
#include<string>
int * fun()
{
int* p = new int(10);
return p;
}
void test01()
{
int *p=fun();
cout << *p << endl;
cout << *p << endl;
delete p;
}
void test02()
{
int * arr = new int[10];
for (int i = 0; i < 10; i++)
{
arr[i] = 100 + i;
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " " ;
}
}
int main()
{
test01();
test02();
}
释放的时候有点区别:delete []arr;delete时 不论在哪一步,立即释放。