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)
4

2 回答 2

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)

至于为什么这对你以前有用,上面的代码在许多地方导致了未定义的行为。有时你不走运,这似乎工作正常。

于 2013-08-29T12:14:30.570 回答
3

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

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

if(argc != 3) 
{
    printf("Error! Type: %s input_file output_file\n", argv[0]);
    exit(1);
}
于 2013-08-29T12:15:29.970 回答