如何正确删除分配的数组(队列数据结构)

问题描述 投票:0回答:1

我使用结构和动态分配的数组创建了队列数据结构,我不知道在没有任何内存泄漏的情况下释放或删除它的正确方法是什么。

我尝试使用以下内容:

删除[] q->数据;

删除[]&(q->数据);

删除&(q-> data);

#include "queue.h"

void initQueue(queue* q, unsigned int size)
{
    q->maxSize = size;
    q->size = 0;
    q->data = new unsigned int[size];
    q->front = 0;
    q->rear = 0;
}

void enqueue(queue* q, unsigned int newValue)
{
    if (q->size != q->maxSize)
    {
        q->data[q->rear] = newValue;
        q->size++;
        q->rear++;
    }
    else
    {
        std::cout << "Queue is full! you can clean it and initialize a new one" << std::endl;
    }
}

int dequeue(queue* q)
{
    int i = 0;

    if (q->size == 0)
    {
        std::cout << "Queue is empty!" << std::endl;
        return EMPTY;
    }
    else
    {
        q->front++;
        q->size--;

        return q->data[q->front];
    }
}

void cleanQueue(queue* q)
{
    //the delete function
}

我使用结构和动态分配的数组创建了队列数据结构,我不知道在没有任何内存泄漏的情况下释放或删除它的正确方法是什么。我已经尝试过使用...

c++ arrays queue delete-operator
1个回答
0
投票

这里的技术正确答案是删除q->数据,正如其他人所建议的那样。但是...

© www.soinside.com 2019 - 2024. All rights reserved.