2013-08-29 78 views
1

昨天在编程期间一切都还好,但今天我得到了奇怪的错误。我不知道为什么,但运行我的程序后,在终端中我得到这个错误“中止(核心转储)”,我也运行已经完成的程序,问题是一样的。该计划的 例子:“中止(核心转储)”在所有程序中

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#define CHUNK 12 

char *getWord(FILE *infile); 

int main(int argc, char *argv[]) 
{ 
    char *word; 
    FILE *infile, *outfile; 
    int n = 0; 

    if(argc != 2) 
    { 
     printf("Error! Type:./file_name input_file output_file\n"); 
     abort(); 
    } 

    infile = fopen(argv[1], "r"); 
    if(infile != NULL) 
    { 
     outfile = fopen(argv[2], "w"); 
     if(outfile == NULL) 
     { 
      printf("Error! Cannot open the output_file\n"); 
      abort(); 
     } 
     else 
     { 
      while(!feof(infile)) 
      { 
       word = getWord(infile); 
       if(word == NULL) 
       { 
        free(word); 
        abort(); 
       } 

       n++; 
       if(n % 2 == 0) 
       { 
        fputs(word, outfile); 
        fputs(" ", outfile); 
       } 
       else 
       { 
        fputs(word, outfile); 
        fputs("(", outfile); 
        fputs(word, outfile); 
        fputs(")", outfile); 
        fputs(" ", outfile); 
       } 
        free(word); 
      } 
     } 
    } 
    else 
    { 
     printf("Error! Cannot open the input_file\n"); 
     abort(); 
    } 

    fclose(infile); 
    fclose(outfile); 
    return 0; 
} 

char *getWord(FILE *infile) 
{ 
    char *word, *word2; 
    int length, cursor, c; 

    word = malloc(sizeof(char)*CHUNK); 
    if(word == NULL) 
    { 
     return NULL; 
    } 

    length = CHUNK; 
    cursor = 0; 

    while(isalpha(c = getc(infile)) && !feof(infile)) 
    { 
     word[cursor] = c; 
     cursor++; 

     if(cursor >= length) 
     { 
      length += CHUNK; 
      word2 = realloc(word, length*sizeof(char)); 
      if(word2 == NULL) 
      { 
       free(word2); 
       return NULL; 
      } 
      else word2 = word; 
     } 
    } 

    ungetc(c, infile); 
    word[cursor] = '\0'; 
    return word; 
} 

和错误:

Error! Type:./file_name input_file output_file 
Aborted (core dumped) 
+0

1)argc 2)!feof() – joop

+0

使用'valgrind'工具来诊断这样的错误。 – Dariusz

+5

'if(word == NULL){free(word);'yikes。 – trojanfoe

回答

3

如果您的命令需要2个参数,则需要检查argc != 3,因为命令名称本身被视为参数。如果您给了它两个参数,那么您的argc != 2检查失败,并且您收到错误消息,并且由于abort调用而发生核心转储。

而不是abort,应该用非零参数调用exit。例如,

if(argc != 3) 
{ 
    printf("Error! Type: %s input_file output_file\n", argv[0]); 
    exit(1); 
} 
+1

甚至更​​好,使用'argv [0]'作为程序名称 – Jens

+0

@Jens是的。谢谢。 – lurker

4

逻辑在realloc是错误的。

word2 = realloc(word, length*sizeof(char)); 
if(word2 == NULL) 
{ 
    free(word2); 
    return NULL; 
} 
else word2 = word; 

应该

word2 = realloc(word, cursor); 
if(word2 == NULL) 
{ 
    free(word); 
    return NULL; 
} 
word = word2; 

这里有几个变化

  • word开始时有length字节分配所以在它重新分配到相同大小没有意义。跟踪字符串大小的变量是cursor,因此您需要重新分配以匹配其大小。
  • (未成年人)有没有必要使用sizeof(char)帮助计算分配的大小 - 这是保证1
  • 如果realloc失败,你需要free原来的指针,而不是新的(你知道是NULL)。
  • 如果重新分配成功,您的堆单元可能已被移动,而word指向您不拥有的内存。该功能的其余部分在word工作,所以你需要更新它指向新的缓冲区(word2

至于为什么这个曾服务于您,上面的代码会导致不确定的行为在许多地方。有时你很不吉利,而且这看起来工作正常。