我使用结构和动态分配的数组创建了队列数据结构,我不知道在没有任何内存泄漏的情况下释放或删除它的正确方法是什么。
我尝试使用以下内容:
删除[] 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
}
我使用结构和动态分配的数组创建了队列数据结构,我不知道在没有任何内存泄漏的情况下释放或删除它的正确方法是什么。我已经尝试过使用...
这里的技术正确答案是删除q->数据,正如其他人所建议的那样。但是...