C++内存模型学习-内存模型

本文主要介绍了C++中堆区和栈区的内存管理方法,包括如何使用new和delete关键字来手动分配和释放堆区内存,以及探讨了返回局部变量地址的问题和潜在风险。
#include "iostream"

using namespace std;

int *test() {
    //利用new关键字,可以将数据开辟到堆区
    //指针 本质也是局部变量,放在栈上,指针保存的数据时放到堆区
    int *p = new int(10);
    return p;
}

int main() {
    int *p = test();
    cout << *p << endl;
    cout << *p << endl;
    return 0;
}

/*
10
10
返回结果多次输出,结果一致
*/

 

 

 

 

 

#include "iostream"

using namespace std;

int *test(int b) {//形参也会放在栈区
    int a = 10;//局部变量,内存放在栈区,栈区的数据在函数执行完成后自动释放
    return &a;//返回局部变量的地址
}

int main() {
    int *p = test(10);
    cout << *p << endl;//第一次可以正常打印结果,是因为编译器做了保留
    cout << *p << endl;//第二次这个数据就不再保留
    return 0;
}

/*
10
32767

 所以不要返回局部变量的地址
*/

 

 

#include "iostream"

using namespace std;

int *test() {
    //利用new关键字,可以将数据开辟到堆区
    //指针 本质也是局部变量,放在栈上,指针保存的数据时放到堆区
    int *p = new int(10);
    return p;
}

int main() {
    int *p = test();
    cout << *p << endl;
    cout << *p << endl;
    return 0;
}

/*
10
10
返回结果多次输出,结果一致
*/

 

 上图意思就是栈区指针保存了堆区的地址,值保存在堆区

#include "iostream"

using namespace std;
//1 new的 基本语法

int *test() {
    //在堆区创建整型数据
    //new返回的是该数据类型的指针
    int *p = new int(10);
    return p;
}

//2 在堆区利用new开辟数组

int *test2() {
    int *arr = new int[10];
    for (int i = 0; i < 10; ++i) {
        arr[i] = i;
    }
    for (int j = 0; j < 10; ++j) {
        cout << arr[j] << endl;
    }
    //释放堆区数组,需要加[]
    delete[] arr;
}


int main() {
    int *p = test();
    cout << *p << endl;
    cout << *p << endl;
    cout << *p << endl;
    //堆区的数据由程序员自己管理开辟,程序员管理释放
    //如果想释放堆区的数据,利用关键字delete
    delete p;
    //cout << *p << endl;//内存已经释放,再次访问就是非法操作,会报错

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值