简体   繁体   English

C内存管理->队列

[英]C Memory Management -> Queue

Okay so i have this Queue implementation that works, but i'm having some memory leak that doesn't let a if else operations runs after that. 好的,所以我有这个可以执行的Queue实现,但是我遇到了一些内存泄漏,该泄漏不允许在此之后运行其他操作。

File queue_arr.h 文件queue_arr.h

#define MaxQ 100

typedef struct {
    int array[MaxQ];
    int front;
    int back;
    int size;
}*Queue;


Queue init();
int enqueue(Queue q, int v);
int dequeue(Queue q, int *v);
int front(Queue q, int *v);
int isEmpty(Queue q);

File queue_arr.c 文件queue_arr.c

#include "queue_arr.h"
#include <stdlib.h>
#include <stdio.h>


Queue init() {
    Queue q = (Queue)malloc(sizeof(Queue));
    q->front = 0;
    q->back = 0;
    q->size = 0;
    return q;
}

int enqueue(Queue q, int v) {
    if(q->size==MaxQ)
        return 0;
    q->array[q->back] = v;
    q->back++;
    if(q->back == MaxQ)
        q->back = 0;
    q->size++;
    return 1;

}

int dequeue(Queue q, int *v) {
    if(q->size == 0)
        return 0;
    *v = q->array[q->front];
    q->front++;
    if(q->front == MaxQ)
        q->front =0;
    q->size--;
    return 1;   
}

int front(Queue q, int *v) {
    if(q->size==0)
        return 0;
    *v = q->array[q->front];
    return 1;   
}

int isEmpty(Queue q) {
    if(q->size == 0)
        return 1;
    return 0;
}

int main(){
    Queue teste = init();
    int *aux;
    *aux = -1;
    printf("Value : %d\n",*aux );

    enqueue(teste,5);
    enqueue(teste,10);
    enqueue(teste,15);
    front(teste,aux);

    printf("Next costumer: %d\n",*aux );

    dequeue(teste,aux);

    printf("Costumer %d left queue\n",*aux );

    dequeue(teste,aux);

    printf("Costumer %d left queue\n",*aux );

    dequeue(teste,aux);

    printf("Costume %d left queue\n",*aux );

    int random = 10;
    if(random == 10)
        printf("asdasdasd\n");


}

The last if and else statement doest not work, i noticed cause i was trying to do a isEmpty if clause, and keep leading me to segmentation fault. 最后的if and else语句不起作用,我注意到原因是我试图执行isEmpty if子句,并一直导致我出现分段错误。

Without the last three lines, the code compiles and runs, with no errors. 没有最后三行,代码可以编译并运行,没有错误。 But with that i just keep getting a segmentation fault. 但是与此同时,我一直在遇到细分错误。

Does anybody knows the problem ? 有人知道这个问题吗?

Your code can't decide whether a Queue is a pointer to something or the something. 您的代码无法确定Queue是指向某物还是某物的指针。

typedef struct {
    int array[MaxQ];
    int front;
    int back;
    int size;
}*Queue;

This says a Queue is a pointer to a bunch of things. 这表示Queue是指向一堆东西的指针。

Queue q = (Queue)malloc(sizeof(Queue));
q->front = 0;

This allocates enough bytes to hold a Queue , which is just a pointer, and then attempts to use the thing it points to. 这会分配足够的字节来保存Queue ,这只是一个指针,然后尝试使用它指向的对象。 But you never allocated space for anything but a pointer. 但是,除了指针之外,您从未分配过空间。

Queue q = (Queue)malloc(sizeof(Queue)); 

祝贺您, sizeof(Queue)是4(或8)个字节,因为它是一个指针。

Your segmentaion fault because of this 由于这个原因,您的细分错误

int *aux;

*aux = -1;

you should 你应该

aux = malloc(sizeof(int));

before 之前

*aux = -1;

but I don't think that is what you want, instead you should just do it 但我认为这不是您想要的,相反,您应该这样做

int aux = -1;

and when calling dequeue 当调用dequeue

dequeue(teste, &aux);

Also I would recomend you fix these other issues 我也建议您解决其他问题

  1. You don't need to cast malloc 您不需要malloc
  2. You should check that malloc didn't return NULL otherwise you will dereference a NULL pointer which is undefined behavior. 您应检查malloc没有返回NULL否则将取消引用未定义行为的NULL指针。
  3. Also, your malloc call is passing the wrong size. 另外,您的malloc调用传递了错误的大小。 A possible fix is to use 可能的解决方法是使用

     Queue q = malloc(sizeof(*q)); 

    But I would actually recommend to avoid hiding the fact that variables are pointers completely. 但是我实际上建议避免隐藏变量完全是指针这一事实。

  4. You do have a memory leak but that is not a cause for a segmentaion fault, you should just call free(teste) at the end of main() . 您确实有内存泄漏,但这不是导致段错误的原因,您应该只在main()末尾调用free(teste) main()
  5. Your main() doesn't return you should, also add return 0; 您的main()不返回您应该return 0; ,还添加了return 0; at the end of main() after free() free()之后main()的末尾

I fixed your code, hope this helps 我修复了您的代码,希望对您有所帮助

#include <stdlib.h>
#include <stdio.h>

#define MaxQ 100

typedef {
    int array[MaxQ];
    int front;
    int back;
    int size;
} *Queue;


Queue init() {
    Queue q = malloc(sizeof(*q));
    if (q == NULL)
        return NULL;
    q->front = 0;
    q->back = 0;
    q->size = 0;
    return q;
}

int enqueue(Queue q, int v) { 
    if (q == NULL)
        return 0;
    if(q->size==MaxQ)
        return 0;
    q->array[q->back] = v;
    q->back++;
    if(q->back == MaxQ)
        q->back = 0;
    q->size++;
    return 1;

}

int dequeue(Queue q, int *v) {
    if (q == NULL)
        return 0;
    if(q->size == 0)
        return 0;
    *v = q->array[q->front];
    q->front++;
    if(q->front == MaxQ)
        q->front =0;
    q->size--;
    return 1;
}

int front(Queue q, int *v) {
    if (q == NULL)
        return 0;
    if(q->size==0)
        return 0;
    *v = q->array[q->front];
    return 1;
}

int isEmpty(Queue q) {
    if (q == NULL)
        return 0;
    if(q->size == 0)
        return 1;
    return 0;
}

int main(){
    Queue teste = init();
    if (teste == NULL)
        return -1;
    int aux;

    aux = -1;
    printf("Value : %d\n", aux);

    enqueue(teste,5);
    enqueue(teste,10);
    enqueue(teste,15);
    front(teste, &aux);

    printf("Next costumer: %d\n", aux );

    dequeue(teste, &aux);

    printf("Costumer %d left queue\n", aux );

    dequeue(teste, &aux);

    printf("Costumer %d left queue\n", aux );

    dequeue(teste, &aux);

    printf("Costume %d left queue\n", aux );

    int random = 10;
    if(random == 10)
        printf("asdasdasd\n");
    free(teste);

    return 0;
}

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

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