리눅스로 큐를 구현하는데 세그멘테이션 오류가 뜹니다
글쓴이: i1004gy / 작성시간: 월, 2021/04/12 - 1:12오전
링크드리스트로 lock을 거는큐를 구현하고하고있는데
코드는 평범한 링크드리스트로 구현하는 queue이고
차이점은 lock을 큐를 인큐할 때와 디큐 할때 건다는 것만 다릅니다
Queue_Enqueue(&q,3);
이걸 쓰니까 오류가 생깁니다
이걸 지우면 오류가 안생기는데
어느 줄이 문제인질 모르니까 잘 모르겠네요
왜 세그멘테이션 오류가 뜨는건가요?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <assert.h>
typedef struct _node_t{
int value;
struct _node_t *next;
} node_t;
typedef struct _queue_t{
node_t *head;
node_t *tail;
pthread_mutex_t headLock;
pthread_mutex_t tailLock;
}queue_t;
void Queue_Iint(queue_t *q){
node_t *tmp = malloc(sizeof(node_t));
tmp->next = NULL;
q->head = q-> tail =tmp;
pthread_mutex_init(&q->headLock, NULL);
pthread_mutex_init(&q->tailLock,NULL);
}
void Queue_Enqueue(queue_t *q, int value){
node_t *tmp = malloc(sizeof(node_t));
assert(tmp !=NULL);
tmp->value =value;
tmp -> next = NULL;
pthread_mutex_lock(&q->tailLock);
q->tail->next = tmp;
q->tail=tmp;
pthread_mutex_unlock(&q->tailLock);
}
int Queue_Dequeue(queue_t *q, int *value){
pthread_mutex_lock(&q->headLock);
node_t *tmp = q->head;
node_t *newHead =tmp->next;
if(newHead ==NULL){
pthread_mutex_unlock(&q->headLock);
return -1;
}
*value = newHead->value;
q->head = newHead;
pthread_mutex_unlock(&q->headLock);
int re=tmp->value;
free(tmp);
return re;
}
int main(){
node_t n;
queue_t q;
Queue_Enqueue(&q,3);
}Forums:


int Queue_Dequeue(queue_t *q,
파라미터 타입이 int* 인데 3을 넣으셨네요
이런식이 되어야겟죠?
Queue_Iint 호출 어디 갔습니까
Queue_Iint 호출 어디 갔습니까
댓글 달기