小编Aha*_*han的帖子

混淆C++ prime加上动态数组的例子

我目前正在阅读C++ prime plus以学习动态内存分配.我在书上尝试了一段代码,但是在我添加了一行之后,我对所得到的内容感到困惑:在我释放p3的内存并且计算机实际打印出来后,我让计算机打印出p3 [2] p3的正确值.但是,在您释放内存后,它是不是不可能打印出来?

这是代码:

// arraynew.cpp -- using the new operator for arrays
#include <iostream>
int main() {
    using namespace std;
    double * p3 = new double [3]; // space for 3 doubles
    p3[0] = 0.2; // treat p3 like an array name
    p3[1] = 0.5;
    p3[2] = 0.8;

    cout << "p3[1] is " << p3[1] << ".\n";
    p3 = p3 + 1; // increment the pointer
    cout << "Now p3[0] is " << p3[0] << " and …
Run Code Online (Sandbox Code Playgroud)

c++ memory arrays dynamic

1
推荐指数
1
解决办法
69
查看次数

标签 统计

arrays ×1

c++ ×1

dynamic ×1

memory ×1