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;
}


转自:https://blog.csdn.net/qq_39328273/article/details/107014918
分类: 杂谈

0 条评论

发表评论

电子邮件地址不会被公开。