C语言多队列创建异常求助:仅成功创建单个队列
Looks like you've hit a handful of key problems with your multi-queue setup—let's break them down and get your code working properly step by step:
1. Misaligned MsgQs_t Structure & Initialization
Right now, your MsgQs_t holds an array of queue pointers, but you're only allocating a single instance of MsgQs_t with initializeMsgQs_t(). When you try to use qs[i] in main(), you're accessing out-of-bounds memory because qs isn't an array of MsgQs_t objects—it's just a pointer to one.
What you actually need is a single MsgQs_t that manages all your queues (each with a unique ID), not an array of MsgQs_t instances.
2. Created Queues Aren't Stored in Your Manager
When you call pq = createQ(qs[i].id);, you never save this new queue pointer into the qs->qs array in your manager struct. That means your created queues are just floating in memory (a memory leak waiting to happen!) and your list function can't find them.
3. Mismatched Function Declaration & Definition
You're calling createQ() with an ID parameter, but your function definition doesn't accept any arguments. This will throw a compile error.
4. Undeclared Variable
The choice variable in main() isn't declared—you'll need to add char choice; at the top of main() to fix this.
Fixed Code Example
Here's the adjusted code with all issues resolved, plus a sample queue-listing function to verify your queues are being created:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_MESSAGES 5 #define MAX_MSG_LEN 20 #define MAX_QUEUES 5 typedef struct queue{ int front, rear, size; char elements[MAX_MESSAGES][MAX_MSG_LEN]; } queue_t; // Revised: This struct manages ALL queues, tracking their IDs and pointers typedef struct MsgQs{ int queue_count; // Track how many queues we've created int queue_ids[MAX_QUEUES]; // Store unique IDs for each queue queue_t *queues[MAX_QUEUES]; // Store pointers to each queue instance } MsgQs_t; // Forward function declarations MsgQs_t* initializeMsgQs(); queue_t* createQ(); void listQueues(MsgQs_t *manager); int main(){ char choice; // Fixed: Declare missing variable int queue_id; int i = 0; MsgQs_t *queue_manager = initializeMsgQs(); queue_t *new_queue = NULL; while(1){ printf("\n1) Create new queue"); printf("\n2) List all queues"); printf("\n0) Quit"); printf("\nEnter choice: "); scanf(" %c", &choice); getchar(); switch(choice){ case '1': if (queue_manager->queue_count >= MAX_QUEUES) { printf("\nError: Maximum number of queues (%d) reached!\n", MAX_QUEUES); break; } printf("\nCreating a queue!\nEnter Queue-ID (Ex. 1234): "); scanf("%d", &queue_id); // Create queue and store it in the manager new_queue = createQ(); queue_manager->queue_ids[i] = queue_id; queue_manager->queues[i] = new_queue; queue_manager->queue_count++; printf("Queue with ID %d has been created successfully!\n", queue_id); i++; break; case '2': listQueues(queue_manager); break; case '0': printf("\nQuitting...\n"); // Add memory cleanup here if needed exit(0); default: printf("Incorrect choice. Re-enter.\n"); break; } } } MsgQs_t* initializeMsgQs(){ MsgQs_t* manager = malloc(sizeof(MsgQs_t)); if (manager == NULL) { printf("Memory allocation failed!\n"); exit(1); } manager->queue_count = 0; // Start with zero queues memset(manager->queue_ids, 0, sizeof(manager->queue_ids)); memset(manager->queues, 0, sizeof(manager->queues)); return manager; } queue_t* createQ(){ queue_t* new_queue = malloc(sizeof(queue_t)); if (new_queue == NULL) { printf("Queue memory allocation failed!\n"); exit(1); } new_queue->front = 0; new_queue->rear = 0; new_queue->size = 0; return new_queue; } // Example function to list all created queues void listQueues(MsgQs_t *manager){ if (manager->queue_count == 0) { printf("\nNo queues created yet.\n"); return; } printf("\nList of created queues:\n"); for (int j = 0; j < manager->queue_count; j++) { printf("Queue ID: %d | Queue address: %p\n", manager->queue_ids[j], manager->queues[j]); } }
Optional Enhancement: Store ID Directly in Queue
If you want each queue to hold its own ID instead of tracking IDs separately in the manager, modify the queue_t struct like this:
typedef struct queue{ int id; // Add queue ID here int front, rear, size; char elements[MAX_MESSAGES][MAX_MSG_LEN]; } queue_t;
Then update createQ to accept an ID parameter:
queue_t* createQ(int id){ queue_t* new_queue = malloc(sizeof(queue_t)); if (new_queue == NULL) { printf("Queue memory allocation failed!\n"); exit(1); } new_queue->id = id; // Assign the ID to the queue new_queue->front = 0; new_queue->rear = 0; new_queue->size = 0; return new_queue; }
And in main(), call it like this:
new_queue = createQ(queue_id); queue_manager->queues[i] = new_queue;
This way, you can remove the separate queue_ids array from MsgQs_t—just access queue->id directly when listing queues.
内容的提问来源于stack exchange,提问作者Mr Mosby

