【问题标题】:Trouble with dynamic input program in CC中动态输入程序的问题
【发布时间】:2013-04-30 22:05:40
【问题描述】:

我正在调试我的程序,但我似乎找不到任何答案。我的程序接收一个文件,将单词复制到一个动态数组中,并为多个单词保留一个字数。

问题 1) 对于我编译的内容,我尝试了不同的输入示例。一个是“foo bar bat bam”,另一个是“foo foo bar bam”。第一个输出是按顺序排列的所有四个单词,第二个输出

    foo
    bar     
    bam
    foo bar bam  

我不知道为什么会这样。

问题 2) 当我尝试将新输入的单词初始化为 1 时出现分段错误。行

arrayOfWords[unique_words].count = 1;

给我一个分段错误。并且使用 -> 不会编译。

问题 3) 我似乎无法动态增长数组。我暂时将它们注释掉,但您可以看到我尝试扩大数组的两种策略。

非常感谢您的帮助!

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_SIZE 10

typedef unsigned int uint;
typedef struct { char * word; int count; } wordType;

int main( void )
{
    wordType *arrayOfWords = (wordType*)malloc(1 * sizeof (wordType) ); 
    wordType *tempArray;
    FILE * inputFile;
    char temp[50];
    uint i;
    uint j;
    uint unique_words;
    uint exists;
    uint wordAdded;
    inputFile = fopen( "input.txt", "r");



    if( inputFile == NULL )
    {
            printf("Error: File could not be opened\n" );
            /*report failure*/
            return 1;
    }
    i = 0;
    unique_words = 0;
    wordAdded = 0;
    while( fscanf( inputFile, "%s", temp) != EOF )
    {


        /*if a word was added, then increase the size by one
        if( wordAdded == 1 )
    {
        tempArray = malloc((unique_words + 1) * sizeof(wordType) );
        memcpy( arrayOfWords, tempArray, unique_words + 1 );
        free( tempArray );
        wordAdded = 0;
    } */

    /*
    if( wordAdded == 1 )
    {
        arrayOfWords = realloc(arrayOfWords, unique_words + 1 );
        wordAdded = 0;
    }*/

    exists = 0;
    for( j = 0; j < unique_words; j++ )
    {
        if( strcmp( arrayOfWords[j].word, temp ) == 0 )
        {
            arrayOfWords[j].count++;
            exists = 1;
        }
    }
    if( exists == 0 )
    {
        arrayOfWords[unique_words].word = malloc(sizeof(char)
                                * (strlen(temp)+1));
        strcpy( arrayOfWords[unique_words].word, temp );
        /*arrayOfWords[unique_words].count = 1; */
        unique_words++;
        wordAdded = 1;
    }
    i++;
}
    printf("unique_words = %d\n", unique_words);
    for( i = 0; i < unique_words; i++ )
    printf("%s\n", arrayOfWords[i].word);


    fclose( inputFile );
   /* for( i = 0; i < size; i++ )
        free( arrayOfWords[0].word );*/
    return 0;
}

【问题讨论】:

    标签: c file-io segmentation-fault malloc


    【解决方案1】:
    int main( void ){
        wordType *arrayOfWords = NULL;
        FILE * inputFile = stdin; //stdin for simplification
        char temp[50];
        uint i,j;
        uint unique_words;
        uint exists;
    
        unique_words = 0;
        while( fscanf( inputFile, "%s", temp) != EOF ){
            exists = 0;
            for( j = 0; j < unique_words; j++ ){
                if( strcmp( arrayOfWords[j].word, temp ) == 0 ){
                    arrayOfWords[j].count++;
                    exists = 1;
                    break;
                }
            }
            if( exists == 0){//new word
                arrayOfWords = realloc(arrayOfWords, (unique_words+1)*sizeof(wordType));
                arrayOfWords[unique_words].count = 1;
                arrayOfWords[unique_words].word = malloc(sizeof(char)*(strlen(temp)+1));
                strcpy(arrayOfWords[unique_words].word, temp );
                ++unique_words;
            }
        }
        printf("unique_words = %d\n", unique_words);
        for( i = 0; i < unique_words; i++ )
            printf("%s\n", arrayOfWords[i].word);
    
        /* deallcate
        for( i = 0; i < unique_words; ++i)
            free( arrayOfWords[i].word );
        free(arraOfWords);
        */
        return 0;
    }
    

    【讨论】:

    • 感谢您的帮助,这澄清了很多。根据需要分配而不是初始化更有意义。
    【解决方案2】:

    你注释掉了重新分配,因为它不起作用,现在它崩溃了,因为你重新分配。

    就像malloc 一样,realloc 函数需要以字节为单位的大小。因此,您应该使用例如

    arrayOfWords = realloc(arrayOfWords, sizeof(wordType) * (unique_words + 1));
    

    当你让这个重新分配工作时,你的程序应该不会再崩溃了。


    如果您想知道,崩溃是因为您增加了unique_words,但没有重新分配缓冲区。这会导致您访问您分配的内存之外的内存,这是未定义的行为,并可能导致崩溃(或其他奇怪的行为)。

    【讨论】:

      猜你喜欢
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多