简体   繁体   English

删除资源

[英]Deleting resources

I'm a beginner in C. I currently have a task to create a program having multiple queues.我是 C 的初学者。我目前有一个任务来创建一个具有多个队列的程序。 How should i correct this?我应该如何纠正这个问题? From my understanding, is supposed to clear all of the queues that where created.根据我的理解,应该清除所有创建的队列。 As currently i think i have memory leaks.目前我认为我有内存泄漏。

#include <stdio.h> //printf etc
#include <stdlib.h> //malloc calloc realloc free
#include <stdint.h>

/* number of message queues */
#define MSGQS_LEN 5

/* number of nodes in the message queue */
#define CAPACITY 5

typedef struct _node {
    const char* message;
    struct _node* next;
} node_t;

typedef struct {
    char qName;
    node_t *front, *rear;
} msg_queue_t;


typedef struct {
    msg_queue_t **queues;
} MsgQs_t;

Your code has several problems.您的代码有几个问题。

if(msg_queues < 0)
        exit(EXIT_FAILURE);

This test is not correct, msg_queues will be NULL if malloc failed for some reason, the test should read.这个测试是不正确的,如果 malloc 由于某种原因失败,msg_queues 将为 NULL,测试应该读取。

if(msg_queues == NULL)
        exit(EXIT_FAILURE);
/* Relinquishes all resources currently held by a MsgQs_t.
   The pointer to the MsgQs_t in question is set to NULL. */
MsgQs_t* unloadMsgQs(){
    MsgQs_t *msg_queues;
    msg_queues = NULL;

    return(msg_queues);
}

You allocate a variable on the stack, initialize it to NULL and return NULL from this function.您在堆栈上分配一个变量,将其初始化为 NULL 并从此函数返回 NULL。

What you actually want to do is pass a MsqQs_t* to unloadMsgQs and use this pointer as an argument to free , something like this您真正想要做的是将 MsqQs_t* 传递给unloadMsgQs并将此指针用作free的参数,就像这样

void unloadMsgQs(MsgQs_t *msg_q) {
    if(msg_q) {
        free(msg_q);
    }
}

If you want to set the msg_q pointer to NULL so that it can't be reused anymore, you should probably do something like.如果你想将 msg_q 指针设置为 NULL 以便它不能再被重用,你可能应该做类似的事情。

void unloadMsgQs(MsgQs_t **msg_q) {
    if(msg_q && *msg_q) {
        free(*msg_q);
        *msg_q = NULL;
    }
}

From what I see, my advice would be to read some more books / tutorials on programming with C and pointers in general, because it seems you don't quite grasp the basics yet (which is nothing to be ashamed of of course!)从我所见,我的建议是阅读更多有关 C 和指针编程的书籍/教程,因为您似乎还没有完全掌握基础知识(当然这没什么可羞耻的!)

You have to call free with the pointer value returned from malloc .您必须使用从malloc返回的指针值调用free For this you have to pass the pointer to unloadMsgQs as an argument.为此,您必须将指向unloadMsgQs的指针作为参数传递。

If this function is supposed to set the pointer to NULL in the caller, you have to pass the address of the pointer.如果该函数应该在调用者中将指针设置为NULL ,则必须传递指针的地址。

Note that malloc 's return value to indicate an error is NULL not a value < 0 .请注意, malloc指示错误的返回值是NULL而不是值< 0

/* Returns a pointer to MsgQs_t structure and through which multiple message queues can be subsequently created.
   Each individual message queue is to be identified by a unique identifier. */
MsgQs_t* initializeMsgQs(){
    MsgQs_t* msg_queues;
    msg_queues = malloc(sizeof(MsgQs_t));

    if(msg_queues == NULL)
        exit(EXIT_FAILURE);

    return(msg_queues);
}


/* Relinquishes all resources currently held by a MsgQs_t.
   The pointer to the MsgQs_t in question is set to NULL. */
void unloadMsgQs(MsgQs_t **msg_queues){

    if(msg_queues != NULL)
    {
        free(*msg_queues);
        *msg_queues = NULL;
    }
}

/* sample use in main() */
int main(int argc, char **argv)
{
    MsgQs_t* msg_queues;

    msg_queues = initializeMsgQs();
    /* ... */
    unloadMsgQs(&msg_queues);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM