首页 > 解决方案 > 未创建多个队列 - C

问题描述

起初我正在创建这个程序来满足一个队列,并且一切正常。我目前正在尝试创建多个队列,方法是使用一个结构数组,每个队列都用自己的 ID 标识。但看起来它仍然只创建一个队列(当我使用一个列出所有队列的函数时,它只列出一个),我需要一些帮助来修复它。代码可以在下面找到:

    #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;

    typedef struct MsgQs{
        int id;
        queue_t *qs[MAX_QUEUES];
    }MsgQs_t;


int main(){
int id, i = 0;

    MsgQs_t *qs = initializeMsgQs_t();
    //MsgQs_t *qs = NULL;
    queue_t *pq = NULL;

while(1){
        printf("\n1) Create new queue");
        printf("\n0) Quit");
        printf("\nEnter choice:");
        scanf(" %c", &choice);
        getchar();

        switch(choice){
            case '1': //createQ
                printf("\nCreating a queue!\nEnter Queue-ID (Ex. 1234):");
                scanf("%d", &qs[i].id);
                pq = createQ(qs[i].id);
                printf("Queue with ID %d has been created successfully!\n", qs[i].id);
                i++;
                break;
            case '0':
                printf("\nQuitting");
                exit(1);
            default:
                printf("Incorrect. Re-enter.\n");
                break;
        }
    }
}

MsgQs_t* initializeMsgQs_t(){
    MsgQs_t* qs = malloc(sizeof(MsgQs_t));

    return qs;
}

queue_t* createQ(){

    queue_t* pq = malloc (sizeof(queue_t));

    pq -> front = 0;
    pq -> rear = 0;
    pq -> size = 0;

    return pq;
}

尽管我最小化了程序以指示我在程序中遇到的第一个问题,但我还有更多关于这些多个队列的功能。

标签: carraysstructqueuemessage-queue

解决方案


这里的这一行正在造成内存泄漏:

pq = createQ(qs[i].id);

createQ正在返回一个指向 的实例的有效指针queue_t并将其分配给pq. 但是,在下一次迭代中,pq将分配给 的新实例queue_tpq只能指向 的单个实例queue_t,因此上一次迭代的实例不再可访问。这是典型的内存泄漏。有几种方法可以解决此问题,但归结为拥有多个指向queue_t. 看起来你对MsgQs_t结构采取了正确的方法。我将提供一个适合您正在尝试做的事情的解决方案。

首先这个定义:

typedef struct MsgQs{
    int id;
    queue_t *qs[MAX_QUEUES];
}MsgQs_t;

当您创建 的实例时,您现在拥有一个固定大小MsgQs_t的数组。createQ 中的in 覆盖了这个 queue_t* 数组。实例化时,您已经为指针分配了空间。这只是对堆栈与堆分配的误解。这是一个更简单的例子。queue_t*MAX_QUEUESmallocMsgQs_tchar*

char* charptr = malloc(10);  /* heap allocation requires malloc */
char chararray[10];          /* This is ALREADY allocated 10 chars on the stack */

所以你可以直接分配给 struct member qs。很难说,但看起来您的意图实际上是id每个队列有 1 个。如果是这种情况,您的结构定义应如下所示:

typedef struct MsgQs{
    int id;
    queue_t *msgQueue; /* changed name to avoid confusion */
}MsgQs_t;

您还想更改malloc大小initializeMsgQs_t

//This allocates MAX_QUEUES MsgQs_t structs
MsgQs_t* qs = malloc(MAX_QUEUES * sizeof(MsgQs_t));

//This only allocates 1:
MsgQs_t* qs = malloc(sizeof(MsgQs_t));

现在,您有一个MAX_QUEUES类型为 struct 的堆分配数组MsgQs_t。然后,您可以直接在循环中分配给它们。下面是循环的重写版本。我更改了 while 循环,因为我们不想循环传递的 MAX_QUEUES。访问qs[MAX_QUEUES]将是未定义的行为,并可能导致分段错误。

while (i < MAX_QUEUES) {
    printf("\n1) Create new queue");
    printf("\n0) Quit");
    printf("\nEnter choice: ");
    scanf("%c", &choice);
    getchar();

    switch(choice) {
        case '1': //createQ
            printf("\nCreating a queue!\nEnter Queue-ID (Ex. 1234):");
            scanf("%d", &qs[i].id);
            getchar();
            qs[i].msgQueue = createQ();
            printf("Queue with ID %d has been created successfully!\n", qs[i].id);
            i++;
            break;
        case '0':
            printf("\nQuitting");
            exit(1);
        default:
            printf("Incorrect. Re-enter.\n");
            break;
    }
}

推荐阅读