c++动态数组常见的一些问题 (delete,以及对数组元素重新修改)

c++中动态分配是常用的一个手段,我们都知道在new完之后就delete,要法口诀背的滚瓜烂熟,正所谓一顿吹嘘猛如虎,一看操作零点五~~~我们在创建的过程中肯定会有一些内存的问题(栈溢出,内存泄露。。。)唉,新手谁用谁知道。下面我用一个昨晚我自己写的实例来说一说我当时的问题。

#include<iostream>
using namespace std;

int main() {
    int pt[] = { 1,2,3,4,5,6,7,8,9 };
    int* p = new int[sizeof(pt) / sizeof(int)]();

    for (int i = 0; i < (sizeof(pt) / sizeof(int)); i++) {

        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
        p++;
        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl << endl << endl;

    }
    cout << endl << endl << endl;
    cout << "*******************分割线**********************" << endl;
   
    cout << endl;
    cout << "now the p is:    " << p << "    the  &p is:   " << &p << endl;
    cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
    cout << "************************************************************************************************" << endl << endl << endl;

    for (int i = 0; i < (sizeof(pt) / sizeof(int)); i++) {
        p[i] = pt[i];
        p++;
    }   
    for (int i = 0; i < (sizeof(pt) / sizeof(int)); i++) {

        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
        p++;
        // cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl << endl << endl;
    }
    delete[]p; 
   system("pause");
    return 0;
}

运行结果如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
开始的时候还好,p是按照正确的方向进行着,但是到了后来对p里面的元素值重新修改的时候发生了意外,没有按照我们预期的目标进行,而是出现了乱码的现象。并且在后面delete的时候触发了断点。

原因就在:
1、

    for (int i = 0; i < (sizeof(pt) / sizeof(int)); i++) {

        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
        p++;
        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl << endl << endl;

    }

在第一个for循环中,p++操作导致指针不在指向原来的地址。
这就导致了后来的delete无法使用,因为他找不到原来的地址了。

2、在输出结果中 p中的数组值也不对,原因是:

    for (int i = 0; i < (sizeof(pt) / sizeof(int)); i++) {

        p[i] = pt[i];
        p++;

    }

在这个循环中,我用p[i]==pt[i]这个操作没问题,但是下面随后就跟着一个p++,这两个操作加起来就等于一个骚操作:p[i+1]=pt[i]…可怜的我当时还没发现,就那么傻傻的调了半天程序

希望我们以后在用指针的时候多注意一点,仔细一些准没错~~~~~~~~~

下面我把修改好的代码粘上来,仅仅当一个示例,写的不好,请各位莫笑,哈哈哈哈~~~~~~~~~


#include<iostream>
using namespace std;

int main() {
    int pt[] = { 1,2,3,4,5,6,7,8,9 };
    int* p = new int[sizeof(pt) / sizeof(int)]();
    int* temp = p;
    cout << "temp=  " << temp << "    *temp=   " << *temp << endl;
    cout << "now the p is:    " << p << "    the  &p is:   " << &p << endl;

    for (int i = 0; i < (sizeof(pt) / sizeof(int)); i++) {

        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
        p++;
        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl << endl << endl;

    }
    cout << endl << endl << endl;
    cout << "*******************分割线**********************" << endl;
    p = temp;
    cout << endl;
    cout << "now the p is:    " << p << "    the  &p is:   " << &p << endl;
    cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
    cout<< "************************************************************************************************" << endl << endl << endl;

    for (int i = 0; i < (sizeof(pt) / sizeof(int)); i++) {

        *p = pt[i];
        p++;

    }
    p = temp;


    for (int i = 0; i < (sizeof(pt) / sizeof(int) ); i++) {

        cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
        p++;
       // cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl << endl << endl;

    }



    //for (int i = 0; i < (sizeof(pt) / sizeof(int)+1); i++) {

    //    cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl;
    //    p++;
    //    //              cout << "the p is:   " << p << "   the *p is:   " << *p << "   the &p is:" << &p << endl << endl << endl;

    //}
    
    //p = temp;
    //delete[]temp;
    p = nullptr;
    delete[]temp;
    cout << endl;
    cout << endl << "temp :   " << temp << "p is :   " << p;
    cout << endl << "&temp is" << &temp << "*temp is:" << *temp;
   // cout << endl << "&p is" << &p << "*p is:" << *p;

    system("pause");
    return 0;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值