首页 > 解决方案 > 代码在 DEV C++ 上运行良好,但在 Hacker Rank 的测试用例中出现分段错误

问题描述

我在 Hacker Rank 的一个测试案例中遇到了分段错误,而其他两个案例工作正常。当我插入与生成错误的测试用例相同的输入时,即使相同的代码在我的系统中也能正常工作(使用 DEV C++)。问题是基于优先级的线程调度。下面给出的是代码:

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct thread_node {
  char name[10];
  int priority;
  struct thread_node *next;
};
typedef struct thread_node thread_node;

struct priority_queue {
  thread_node *front, *rear;
};
typedef struct priority_queue priority_queue;

void enqueue(priority_queue *queue, thread_node *thread) {

  if(queue->rear == NULL){
      queue->front = queue->rear = thread;
      return;
  }
    thread_node *tempFront = queue->front;
    if(queue->rear->priority < thread->priority){
        queue->rear->next = thread;
        queue->rear = thread;
        
    }else{
        if(queue->front == queue->rear){
            thread->next = queue->rear;
            queue->front = thread ; return;
        }
        
        if(thread->priority >= queue->front->priority){
            while(thread->priority >= tempFront->next->priority && tempFront->priority != queue->rear->priority) 
            tempFront = tempFront->next;
            
            thread->next = tempFront->next;
            tempFront->next = thread;
        }
        
        if(thread->priority < queue->front->priority ){
            thread->next = queue->front;
            queue->front = thread; return;
        }
        
    }
}

thread_node* dequeue(priority_queue *queue) {
  thread_node* temp = queue->front;
  
  queue->front = queue->front->next;
  
  return(temp);
}


/*=======DO NOT MODIFY THE CODE BELOW =======*/

thread_node* create_node(char *thread_name, int thread_priority) {
  thread_node *thread;
  thread = (thread_node*)malloc(sizeof(thread_node));
  thread->priority = thread_priority;
  strcpy (thread->name, thread_name);
  thread->next = NULL;

  return thread;
}

//Get new queue
priority_queue* create_queue() {
  priority_queue *queue = (priority_queue*)malloc(sizeof(priority_queue));
  queue->front = NULL;
  queue->rear = NULL;

  return queue;
}

void display(thread_node *thread) {
        printf("%s %d", thread->name, thread->priority);
        printf("\n");
}

int main(int argc, char * argv[])
{
        int thread_count, thread_priority;
        char thread_name[10];

        priority_queue *queue = create_queue();

        scanf("%d", &thread_count);

        thread_node *thread[thread_count];

        //Input the Thread priority table and put it in queue.
        for (int index = 0; index < thread_count; index++) {
        scanf("%s %d",thread_name,&thread_priority);

        thread[index] = create_node(thread_name, thread_priority);

        enqueue(queue, thread[index]);
        }

        for (int index = 0; index < thread_count; index++) {
                //dequeue will return threads
                display(dequeue(queue));
       }
}

输入:10 线程0 6 线程1 4 线程2 9 线程3 1 线程5 9 线程6 2 线程7 4 线程8 3 线程9 6 线程10 2

错误如下:分段错误程序以信号 SIGSEGV 终止,分段错误。队列->前=线程;返回;

标签: csegmentation-faultpriority-queue

解决方案


推荐阅读