动态内存分配的变量使用指针复制给另一个变量如何释放

jeff_
  • 39

问题:
使用new申请了一块空间,然后使用指针给另一个变量,那么该delete哪个变量?

测试代码:(代码来源百度,然后我发现没有delete释放new对象)

cout << "****进行排序****" << endl;
//第一位地址可以存放指示器,从第二位开始保存数据
node *mylist = new node[sizeof(node)];
mylist->data = 0;
mylist->next = NULL;

int len = length(head);
int i = 0;
node * cur = mylist;


node *headcopy = head;
while (len--)
{
    //node * newNode = (node *)malloc(sizeof(node));   
    node *newNode = new node[sizeof(node)];            
    newNode->data = headcopy->data;
    newNode->next = NULL;
    cur->next = newNode;
    cur = cur->next;
    headcopy=headcopy->next;
}
head = insertSort(mylist);
head = del(head, 0);
printL(head);

return 0;

}

我百度看到这么一串代码,原代码是没有写delete的,我私自认为有内存泄露,就自己尝试加上了delete,但是delete newNode这个是没有问题的,但是再delete mylist时,VS提示错误。

因为考虑循环内部的new对象无法delete(对于局部变量,要在所在的模块中delete),于是自己将newNode写在了循环体外。

代码:

cout << "****进行排序****" << endl;
//第一位地址可以存放指示器,从第二位开始保存数据
node *mylist = new node[sizeof(node)];
mylist->data = 0;
mylist->next = NULL;

int len = length(head);
int i = 0;
node * cur = mylist;


node *headcopy = head;
node *newNode = new node[0];
while (len--)
{
    //node * newNode = (node *)malloc(sizeof(node));   
    newNode = new node[sizeof(node)];
    newNode->data = headcopy->data;
    newNode->next = NULL;
    cur->next = newNode;
    cur = cur->next;
    headcopy = headcopy->next;
}
head = insertSort(mylist);
head = del(head, 0);
printL(head);


if (newNode)
    delete[] newNode;

if(mylist)
    delete[] mylist;

return 0;

}

clipboard.png

附加:
1.如果觉得不好解释请直接添加代码解决方案,我在摸索一下。
2.关于new动态内存分配,我记得之前也看过不要使用指针复制之类的,再去百度找不到了,需要注意容易引起内存泄露的情况也请各位解释一下。(PS:当然使用智能指针比较好,但就怕有的版本不支持嘛QAQ)

评论
阅读 999
2 个回答
✓ 已被采纳

newNode = new node[sizeof(node)];

应该是想每次创建一个节点,但是使用 []

似乎是搞错了数组的使用


为何需要 node *newNode=new node[0]; ?有什么特别用处吗?


保存好头指针,释放头指针即可

#include <iostream>
#include <stdio.h>
#include <string>
#include <conio.h>

/**
* cstdio是将stdio.h的内容用C++头文件的形式表示出来。
*stdio.h是C标准函数库中的头文件,即:standard buffered input&output。
*提供基本的文字的输入输出流操作(包括屏幕和文件等)。
*/

/**
*conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,
*主要是一些用户通过按键盘产生的对应操作,比如getch()()函数等等。
*/

using namespace std;

struct node
{
    int data;
    node *next;
};
typedef struct node node, *list;

// 创建单链表
node *creat()
{
    node *head, *p;
    head = new node;
    p = head;

    int x, cycle = 1;
    while (cycle)
    {
        cout << "Please input the data for single linker : ";
        cin >> x;

        if (x != 0)
        {
            node *s = new node;
            s->data = x;
            cout << "Input data : " << x << endl;

            p->next = s;
            p = s;
        }
        else
        {
            cycle = 0;
            cout << "Input done! " << endl;
        }
    }

    head = head->next;
    p->next = NULL;
    //cout << "\nFirst data of single linker is " << head->data << endl;

    return head;
}

// 单链表测长
int length(node *head)
{
    int n = 0;
    node *p = head;

    while (p != NULL)
    {
        p = p->next;
        n++;
    }

    return n;
}

// 单链表打印
void printL(node *head)
{
    node *p = head;

    while (p != NULL)
    {
        cout << "Single Linker data is " << p->data << endl;
        p = p->next;
    }
}

// 单链表插入
node *insert(node *head, int num)
{
    node *p0, *p1, *p2;
    p1 = head;

    p2 = new node;
    p0 = new node; // 插入节点
    p0->data = num;// 插入数据

    while (p0->data > p1->data && p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;// p0,p1和p2位置: p2->p1->p0
    }

    if (p0->data <= p1->data)
    {
        if (p1 == head)
        {// 头部前段插入 p0和p1位置: p0->p1->...
            head = p0;
            p0->next = p1;
        }
        else
        {// 插入中间节点 p0,p1和p2位置: p2-> p0 -> p1
            p2->next = p0;
            p0->next = p1;
        }
    }
    else
    {   // 尾部插入节点 p0,p1和p2位置: p2->p1->p0->NULL
        p1->next = p0;
        p0->next = NULL;
    }
    return head;
}

// 单链表删除
node *del(node *head, int num)
{
    node *p1, *p2;
    p2 = new node;
    p1 = head;

    while (num != p1->data && p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;// p1和p2位置: p2->p1        
    }

    if (num == p1->data)
    {
        if (p1 == head)// 删除头节点
        {
            head = p1->next;
            delete p1;
        }
        else
        {
            p2->next = p1->next;
            delete p1;
        }
    }
    else
    {
        cout << num << " could not been found in the current single linker!" << endl;
    }
    return head;
}

//=============插入排序====================
node *insertSort(node *head)
{
    node  *p1, *prep1, *p2, *prep2, *temp;
    prep1 = head->next;
    p1 = prep1->next;
    //prep1和p1是否需要手动后移
    bool flag;

    while (p1 != NULL)
    {
        flag = true;
        temp = p1;
        //由于是单向链表,所以只能从头部开始检索
        for (prep2 = head, p2 = head->next; p2 != p1; prep2 = prep2->next, p2 = p2->next)
        {
            //发现第一个较大值
            if (p2->data > p1->data)
            {
                p1 = p1->next;
                prep1->next = p1;
                prep2->next = temp;
                temp->next = p2;
                flag = false;
                break;
            }
        }
        //手动后移prep1和p1
        if (flag)
        {
            prep1 = prep1->next;
            p1 = p1->next;
        }
    }
    return head;
}

int main()
{
    cout << "***创建单链表***" << endl;
    node *head = creat();
    cout << endl;

    cout << "***计算链表长***" << endl;
    int n = length(head);
    cout << "The length of input single linker is " << n << "." << endl;
    cout << endl;

    cout << "***打印单链表***" << endl;
    printL(head);
    cout << endl;

    cout << "****插入节点****" << endl;
    cout << "Please input the data for inserting operate : ";
    int inData;
    cin >> inData;
    head = insert(head, inData);
    printL(head);
    cout << endl;

    cout << "****删除节点****" << endl;
    cout << "Please input the data for deleting operate : ";
    int outData;
    cin >> outData;
    head = del(head, outData);
    printL(head);
    cout << endl;

    cout << "****进行排序****" << endl;
    //第一位地址可以存放指示器,从第二位开始保存数据
    node *mylist = new node[sizeof(node)];
    mylist->data = 0;
    mylist->next = NULL;

    int len = length(head);
    int i = 0;
    node * cur = mylist;


    node *headcopy = head;
    //node *newNode = new node[0];
    while (len--)
    {
        //node * newNode = (node *)malloc(sizeof(node));   
        node * newNode = new node;
        newNode->data = headcopy->data;
        newNode->next = NULL;
        cur->next = newNode;
        cur = cur->next;
        headcopy = headcopy->next;
    }
    head = insertSort(mylist);
    head = del(head, 0);
    printL(head);


    //if (newNode)
    //    delete[] newNode;

    //if(mylist)
    //    delete[] mylist;

    while (mylist != NULL) {
        node *t = mylist->next;
        delete mylist;
        mylist = t;
    }

    return 0;
}

这是我的完整代码。麻烦你看一下。QAQ

宣传栏