C++数组实现非环形整数队列执行菜单选项2时异常求助
Hey there! Let's work through the exception you're hitting when selecting menu option 2. Based on the details you shared, there are a couple of key logical flaws in your queue implementation that are almost certainly causing the issue. Let's break them down and fix them step by step:
1. Incorrect isFull() Logic
You mentioned isFull() checks if rear == queue size—but here's the problem: arrays in C++ are zero-indexed. If your queue has a fixed size (say MAX_SIZE), the valid indices range from 0 to MAX_SIZE - 1.
Your rear starts at -1, so when you enqueue elements, rear increments to 0, then 1, and so on. By the time rear reaches MAX_SIZE - 1, the queue is completely full. If you wait until rear == MAX_SIZE to mark it full, you've already tried to write to an out-of-bounds array index, which triggers undefined behavior (like the exception you're seeing).
Fix the isFull() function:
bool isFull() { // Check if rear has reached the last valid array index return rear == MAX_QUEUE_SIZE - 1; }
2. Broken Empty Queue Check
You said you check if rear == front to determine an empty queue—but this doesn't account for the initial state! If front starts at 0 (the standard setup for non-circular queues) and rear starts at -1, rear will never equal front when the queue is empty. This means your code might try to perform operations (like dequeue) on an empty queue, leading to invalid memory access.
Fix the isEmpty() function:
bool isEmpty() { // If rear is before front, the queue has no elements return rear < front; }
This covers all empty scenarios: the initial state (rear=-1 < front=0), and after dequeuing all elements (where front ends up greater than rear).
3. Example Corrected Queue Implementation
Here's a full, simplified version of your queue with these fixes, plus safe enqueue/dequeue logic:
#include <iostream> #define MAX_QUEUE_SIZE 10 using namespace std; class IntQueue { private: int arr[MAX_QUEUE_SIZE]; int front; int rear; public: // Constructor: Initialize front to 0, rear to -1 (empty queue) IntQueue() : front(0), rear(-1) {} bool isEmpty() { return rear < front; } bool isFull() { return rear == MAX_QUEUE_SIZE - 1; } // Enqueue an element (returns true if successful) bool enqueue(int value) { if (isFull()) { cout << "Error: Queue is full!" << endl; return false; } arr[++rear] = value; return true; } // Dequeue an element (stores value in the reference, returns true if successful) bool dequeue(int &outValue) { if (isEmpty()) { cout << "Error: Queue is empty!" << endl; return false; } outValue = arr[front++]; // Optional: Reset front/rear when queue is empty to keep indices clean if (isEmpty()) { front = 0; rear = -1; } return true; } };
4. Why Menu Option 2 Is Causing an Exception
If option 2 is enqueue, your old isFull() check allowed you to write past the array's bounds. With the fixed isFull(), you'll get a clear error message instead of an exception.
If option 2 is dequeue, your old empty check didn't catch the empty state, so you tried to read from an invalid index. The corrected isEmpty() will block this operation before it causes harm.
内容的提问来源于stack exchange,提问作者Malikx

