不确定if语句导致段错误的原因

时间:2018-04-17 21:46:19

标签: c segmentation-fault

我在我的一个函数中有这个if语句,当我用gdb查询它时似乎返回了一个有效的结果,但它仍然最终给了我segfault。< / p>

这是有问题的功能:

/* reads the file and adds each word to the trie
   returns the number of unique words in the file */
int read_file(FILE *in, T_node *root, int *max_len)
{
   char *word = NULL;
   int num_unique = 0;
   int len;
   max_len = 0;

   if(root == NULL)
   {
      perror("Bad root");
      exit(3);
   }

   while ((word = read_long_word(in)) != NULL)
   {
      len = strlen(word);
      /************ segfault here ***********/
      if (len > *max_len)
      {
         *max_len = len;
      }
      num_unique += add_word(root, word);
   }

   return num_unique;
}

以下是我运行它的地方:

/* tests file with some repeated words */
void test_read_file2(void)
{
   FILE *in = fopen("repeated.txt", "r");
   T_node *root = create_T_node();
   int max_len = 0;
   /****** segfault caused by this call *****/
   checkit_int(read_file(in, root, &max_len), 8);
   checkit_int(max_len, 19);
   free_trie(root);
   fclose(in);
}

这是我从gdb得到的:

27            if (len > *max_len)
(gdb) p len
$4 = 5
(gdb) p *max_len
$5 = 0
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402035 in read_file (in=0x605010, root=0x605250, 
    max_len=0x7fffffffe0dc) at fw.c:27
27            if (len > *max_len)
(gdb) p *max_len
$6 = 0
(gdb) p len > *max_len
$7 = 1

正如您在上面看到的那样,当我打印if条件时,它返回true就好了,但无论如何我在该行(27)上出现了分段错误。我错过了什么?

1 个答案:

答案 0 :(得分:2)

checkboxGroupInput

midterm <- read.csv('midterm-results.csv') library(dplyr) library(tidyr) # get column number for response time k <- c(33:88) v <- c() for (i in k){ if (i%%2 == 1){ v <- c(v,i) } } #average response time by question time <- midterm[ , v] new.col.name <- gsub('_.*', "", colnames(time)) colnames(time) <- new.col.name avg.time <- data.frame(apply(time, 2, mean)) avg.time$question <- rownames(avg.time) colnames(avg.time) <- c('response_time', 'question') rownames(avg.time) <- NULL avg.time$question <- factor(avg.time$question, levels = c('Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8.9', 'Q10','Q11','Q12.13','Q14','Q15','Q16','Q17', 'Q18','Q19','Q20','Q21','Q22','Q23','Q24','Q25', 'Q26','Q27','Q28','Q29','Q30')) avg.time$question_type <- c(1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0) # I did this manually because the there when data was imported into the midterm.csv, # q8 & 9, q12 &13 were accidentally merged (28 v.s 30 question) avg.time$question_type <- ifelse(avg.time$question_type == 1, 'googleable', 'not googleable') avg.time$question_type <- factor(avg.time$question_type, levels = c('googleable', 'not googleable')) library(shiny) library(ggplot2) ui <- fluidPage( checkboxGroupInput(inputId = "type", label = "select question type", choices = levels(avg.time$question_type), selected = TRUE), plotOutput('bar') ) server <- function(input, output) { output$bar <- renderPlot({ ggplot(avg.time[avg.time$question_type==input$type, ], aes(x=question, response_time)) + geom_bar(aes(fill = question_type), stat='identity', width = 0.5) }, height =500, width = 1000) } shinyApp(ui = ui, server = server) 是一个指针

int read_file(FILE *in, T_node *root, int *max_len)

这一行使max_len成为空指针。

max_len = 0;

在这里,您尝试取消引用空指针。

max_len的初始化更改为

*max_len = len;