c中的循环队列函数中的SIGSEGV错误
SIGSEV error in Circular queue function in c
大家好,我一直在尝试为我的项目创建循环队列库 purpose.But,同时在空函数下从 if 语句开发 SIGEGV 错误,每当我在遇到的特定 if 条件下使用条件时这个错误。
#include <stdio.h>
#define SIZE 5
typedef struct queue
{
int values[SIZE];
int head;
int tail;
int size;
int count;
int full_flag;
}queue_t;
int init(queue_t* q)
{
//q->values;
q->head=-1;
q->tail=-1;
q->size=SIZE;
q->count=1;
q->full_flag=0;
}
int empty(queue_t* q) //This if statement where i am getting problem at
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int enqueue(queue_t* q,int input)
{
if(empty(q)==1)
{
printf("Queue is full\n");
}
else
{
if(q->head=-1)
{
q->head=0;
}
q->tail=(q->tail+1)%q->size;
q->values[q->tail]=input;
q->count++;
}
}
int deque(queue_t* q)
{
int result;
q->head=q->head%q->size;
result=q->values[q->head];
q->head++;
printf("Removing from %d\n",result);
q->count--;
}
int main(void) {
queue_t* q;
//q->head=5;
// q->values[2]=3;
init(q);
enqueue(q,2);
enqueue(q,3);
enqueue(q,4);
enqueue(q,5);
enqueue(q,6);
//printf("%d\n",q->values[q->tail]);
//printf("%d",q->head);
// your code goes here
return 0;
}
当我在没有那个 if 循环的情况下执行时,一切正常 fine.BUt 有了它,一切都变成了 chaos.Please 伙计们帮助我提前谢谢
您在主函数中定义了一个指针,但没有为其分配内存
queue_t* q;
这是一个野生(悬挂)指针,因为它不指向特定的内存块。您必须为其分配内存。像这样:
queue_t* q = malloc(sizeof(queue_t));
或
queue_t q;
queue_t* pq = &q;
大家好,我一直在尝试为我的项目创建循环队列库 purpose.But,同时在空函数下从 if 语句开发 SIGEGV 错误,每当我在遇到的特定 if 条件下使用条件时这个错误。
#include <stdio.h>
#define SIZE 5
typedef struct queue
{
int values[SIZE];
int head;
int tail;
int size;
int count;
int full_flag;
}queue_t;
int init(queue_t* q)
{
//q->values;
q->head=-1;
q->tail=-1;
q->size=SIZE;
q->count=1;
q->full_flag=0;
}
int empty(queue_t* q) //This if statement where i am getting problem at
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int enqueue(queue_t* q,int input)
{
if(empty(q)==1)
{
printf("Queue is full\n");
}
else
{
if(q->head=-1)
{
q->head=0;
}
q->tail=(q->tail+1)%q->size;
q->values[q->tail]=input;
q->count++;
}
}
int deque(queue_t* q)
{
int result;
q->head=q->head%q->size;
result=q->values[q->head];
q->head++;
printf("Removing from %d\n",result);
q->count--;
}
int main(void) {
queue_t* q;
//q->head=5;
// q->values[2]=3;
init(q);
enqueue(q,2);
enqueue(q,3);
enqueue(q,4);
enqueue(q,5);
enqueue(q,6);
//printf("%d\n",q->values[q->tail]);
//printf("%d",q->head);
// your code goes here
return 0;
}
当我在没有那个 if 循环的情况下执行时,一切正常 fine.BUt 有了它,一切都变成了 chaos.Please 伙计们帮助我提前谢谢
您在主函数中定义了一个指针,但没有为其分配内存
queue_t* q;
这是一个野生(悬挂)指针,因为它不指向特定的内存块。您必须为其分配内存。像这样:
queue_t* q = malloc(sizeof(queue_t));
或
queue_t q;
queue_t* pq = &q;