新建和删除混淆

New and delete mix ups

本文关键字:删除 新建      更新时间:2023-10-16

我在使用 newdelete 运算符时遇到了一个小问题。我在很多地方读到每个new运算符都必须对应于一个delete,据我了解,用new创建的变量将一直存在,直到它们被该delete击中为止。如果您愿意查看以下代码,它很长但很简单:

#include <iostream>
using namespace std;
int* testFunction1();
int* testFunction2();
int main(){
    int* ptr1 = testFunction1();
    int* ptr2 = testFunction2();
    cout << *ptr1 << endl; // outputs 5
    cout << *(ptr1 - 1) << endl; // outputs random int
    cout << *ptr2 << endl; // outputs random int
    cout << ptr1 << endl; //prints address of b from testFunction1()
    cout << ptr1 - 1 << endl; // prints address of a and c from testFunction1()
    cout << ptr2 << endl; // prints address of a and c from testFunction1()
    cout << endl;
    // delete ptr1; won't work
    return 0;
}
int* testFunction1(){
    int a = 5, b = 10;
    int* pointerToInt1 = new int;
    pointerToInt1 = &a;
    pointerToInt1 = &b;
    cout << &a << endl;
    cout << &b << endl;
    return pointerToInt1;
}
int* testFunction2(){
    int c = 5;
    int* pointerToInt2 = &c;
    cout << &c << endl;
    return pointerToInt2;
}

我有两个问题:

  1. 我认为,使用 testFunction1() ,我按值返回指针。但是我不知道如何解决这个问题,返回对指针的引用,以便我可以释放 main 方法(或就此而言,任何其他方法)中的内存。

  2. 为什么当我取消引用 5 时得到 5 作为输出*ptr1?我的意思是,从地址输出中,很明显分配给c的值存储在testFunction2(),但为什么会这样呢?

我们不要把你的问题放在一边,先解释你的代码。

声明并定义了一个名为 testFunction1 的函数,该函数返回 int poitner

int* testFunction1(){
    int a = 5, b = 10;             // define local variables with initial values. a,b have different memory addresses
    int* pointerToInt1 = new int;  // dynamic allocate pointer(new address) to int
    pointerToInt1 = &a;            // set pointerToInt1 to point to address of a
    pointerToInt1 = &b;            // set pointerToInt1 to point to address of b
    cout << &a << endl;           
    cout << &b << endl;
    return pointerToInt1;          // return pointerToInt1 pointer which currently points to address of b
}

a,b函数testFunction1中的局部变量,它们具有自动持续时间,当函数完成时,它们被释放,因此pointerToInt1实际上是悬空指针并访问它是未定义的行为。

此外,testFunction1中引入了典型的内存泄漏,由new分配的原始内存块丢失。

int* pointerToInt1 = new int;
pointerToInt1 = &a;

现在,让我们"fix"函数testFunction1,是的,我的意思是用双引号修复。

int* testFunction1(){
    int a = 5, b = 10;             
    int* pointerToInt1 = new int;  
    *pointerToInt1 = a;             // copy a to memory pointerToInt1 
    *pointerToInt1 = b;             // copy b to memory pointerToInt1 
    cout << &a << endl;
    cout << &b << endl;
    return pointerToInt1;           // return pointerToInt1 pointer
}

调用函数 testFunction1 后,仍然需要删除动态分配的内存块。

int *p = testFunction1();
delete p;

现在,如果您回过头来回顾您的两个问题,您会得到答案吗?

您正在设置指向堆栈上分配的变量的指针:

int a = 5, b = 10;
int* pointerToInt1 = new int; // allocates an int on heap
pointerToInt1 = &a; // overwrite pointer with a local variable

发生的情况是,您返回的指针指向在堆栈上自动分配的值,因此当函数退出其范围时,pinter 将变为无效。

此外,由于您丢失了对堆上动态分配的对象的任何引用,因此无法再删除它,因此它是泄漏的内存。

最后,删除尝试删除指向在调用testFunction1期间堆栈上的地址的指针,因此它不再是有效的指针,因此delete不起作用。