在以下代码中执行删除操作时,我无法获得以下代码中返回的正确值
While performing the delete operation in the following code, I am not able to get the correct values returned in the following code.
无法打印队列中已删除元素的正确值。DeQueue()用于删除元素。我为QUEUE使用了动态循环阵列实现技术,即当QUEUE满时,将内存加倍。删除过程中返回的第一个元素是正确的,但在此之后,如果再次执行删除,则返回随机值。
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
struct DynArrayQueue{
int front, rear;
int capacity;
int* array;
};
void ResizeQueue(struct DynArrayQueue* Q);
struct DynArrayQueue* CreateDynQueue(){
struct DynArrayQueue* Q = (struct DynArrayQueue* )malloc(sizeof(struct DynArrayQueue));
if(!Q)
{
cout << "Memory Error in allocation!n";
return NULL;
}
Q->capacity = 1;
Q->front = Q->rear = -1;
Q->array = (int* )malloc(Q->capacity * sizeof(int));
if(!Q->array)
{
cout << "Memory error in creating queue!n";
return NULL;
}
return Q;
}
int IsEmptyQueue(struct DynArrayQueue* Q){
if(Q->front == -1){
cout << "Queue is empty!n";
return 1;
}
else{
cout << "Queue not emptyn";
return 0;
}
}
int IsFullQueue(struct DynArrayQueue* Q){
if((((Q->rear)+1)%Q->capacity) == Q->front)
{
cout << "Queue is full!n";
return 1;
}else{
cout << "Queue not full!n";
return 0;
}
}
void QueueSize(struct DynArrayQueue* Q){
int s;
s = (Q->capacity - Q->front + Q->rear + 1) % Q->capacity;
cout << "Size of the queue is " << s;
cout << "nn";
}
void EnQueue(struct DynArrayQueue* Q){
int data;
cout << "Enter the data to be inserted:n";
cin >> data;
if(IsFullQueue(Q))
{
ResizeQueue(Q);
}
Q->rear = (Q->rear + 1)%Q->capacity;
Q->array[Q->rear] = data;
if(Q->front == -1)
{
Q->front = Q->rear;
}
}
void ResizeQueue(struct DynArrayQueue* Q){
int size = Q->capacity;
Q->capacity = Q->capacity * 2;
Q->array = (int* )realloc(Q->array, Q->capacity);
if(!Q->array)
cout << "Memory error!n";
if(Q->front > Q->rear)
{
for(int i = 0; i < Q->front; i++)
{
Q->array[i+size] = Q->array[i];
}
Q->rear = Q->rear + size;
}
}
void DeQueue(struct DynArrayQueue* Q){ **this function does not work properly**
int data = 0;
if(IsEmptyQueue(Q))
{
cout << "Queue Underflow!n";
cout << "No element to delete!n";
}
else{
data = Q->array[Q->front];
if(Q->front == Q->rear)
{
Q->front = Q->rear = -1;
}
else
{
Q->front = ((Q->front) + 1) % Q->capacity;
}
}
cout << "Element deleted is " << data;
cout << "n";
}
void DeleteQueue(struct DynArrayQueue* Q){
if(Q){
if(Q->array)
{
free(Q->array);
}
free(Q);
}
}
int main()
{
int choice;
struct DynArrayQueue* Q1;
while(1)
{
cout << "1. Press to create a Queue:n";
cout << "2. Enter an element in the queue:n";
cout << "3. Delete an element from the queue:n";
cout << "4. Press to know the Queue size:n";
cout << "5. Press to know if Queue is full:n";
cout << "6. Press to know if Queue is empty:n";
cout << "7. Press enter to exit:n";
cout << "Enter your choice:n";
cin >> choice;
switch(choice)
{
case 1:
Q1 = CreateDynQueue();
break;
case 2:
EnQueue(Q1);
break;
case 3:
DeQueue(Q1);
break;
case 4:
QueueSize(Q1);
break;
case 5:
IsFullQueue(Q1);
break;
case 6:
IsEmptyQueue(Q1);
break;
case 7:
exit(0);
break;
default:
cout << "Wrong choice entered!n";
break;
}
}
DeleteQueue(Q1);
}
问题不在DeleteQueue
,而是在ResizeQueue
。当您调用realloc
时,您传入了新数量的元素,但没有考虑元素的大小(就像最初分配空间时一样)。应该是
Q->array = (int* )realloc(Q->array, Q->capacity * sizeof(int));
问题出现在ResizeQueue()
中。
声明
Q->array = (int* )realloc(Q->array, Q->capacity);
没有分配正确的大小。随后的操作(直到下一个DeleteQueue()
)愉快地假设数组比实际长度长,并写过末尾。
此外,如果realloc()
失败,则返回NULL
并且不释放旧存储器。这个赋值的结果就是内存泄漏(因为Q->array
的旧值在程序中丢失了)。
你实际需要做的(至少)是
int *temp = (int* )realloc(Q->array, Q->capacity * sizeof(int));
if (!temp)
cout << "Memory error!n";
else
Q->array = temp;
即使这样也不够,因为后续代码使用Q->array
并假设调整大小成功。如果发生了故障,结果是未定义的行为。我将把恰当地解决这一问题作为一种学习练习。
此外,在C++中,考虑使用标准容器(如std::vector<int>
),这些容器可以以可控的方式调整自己的大小,这样就可以避免分配错误的内存量等愚蠢行为。
相关文章:
- 为什么这段代码返回 -nan(ind)?C++
- 为什么我的代码返回了错误的平方值?C++
- 从本机代码返回到托管代码会损坏返回的对象
- Fibbonaci 递归代码返回错误值,始终返回下一个数字
- 为什么此代码返回分段错误错误?
- C 语法,根据此代码返回值类型
- 为什么在下面的代码返回类型中是用于运营商重载的类类型
- 为什么下面的使用链表实现线性队列的代码返回垃圾值然后崩溃
- 为什么下面的代码返回向量大小为 10
- 从本机代码返回"const char*"并在java中获取"字符串"
- JNI 代码返回字符串,但得到错误 java.lang.UnsatisfiedLinkError: com.packag
- 相同的数字代码返回不同的输出,无论是 C++ 还是 C
- C/C++ 从 x86 代码返回
- 从 mfc 应用程序点击网络服务,并希望从网络服务登录方法读取用户代码返回
- 代码块 10.05 和 12.11、13.12 使用相同的代码返回不同的结果
- C++将字符串转换为 wstring 并使用最小代码返回
- 混合托管C++方法并不总是向调用C#代码返回相同的结果
- 可以将用于向std::字符串返回const ref的函数更改为通过值break调用代码返回
- 请解释这段代码(返回语句)
- 项目欧拉任务#8在c++中,代码返回错误答案的微妙原因