C语言中的动态分配

C语言中的动态分配,c,arrays,malloc,free,C,Arrays,Malloc,Free,我在动态分配方面有困难。我的程序需要接收一个文本文件,接收每个单词,并将它们放入一个数组中,同时计算重复的单词。我认为我正在将单词正确地malloc到数组中,但是我不知道如何使用我使用动态分配创建的结构创建数组。i、 e.它需要像列表一样增长。谢谢你能给我的任何帮助。我已经注释掉的地方也是麻烦的地方 #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h>

我在动态分配方面有困难。我的程序需要接收一个文本文件,接收每个单词,并将它们放入一个数组中,同时计算重复的单词。我认为我正在将单词正确地malloc到数组中,但是我不知道如何使用我使用动态分配创建的结构创建数组。i、 e.它需要像列表一样增长。谢谢你能给我的任何帮助。我已经注释掉的地方也是麻烦的地方

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


typedef unsigned int uint;

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



/* store the string in another temp string which you can iterate through
and compare with the array of non-repeated words.  if there, increment*/


int main( void ) 
{
    typedef struct wordType * WORD_RECORD;
    WORD_RECORD arrayOfWords = malloc(10 * sizeof( WORD_RECORD) );

    FILE * inputFile;
    char temp[50];
    uint i;
    uint j;
    uint size;
        uint exists;
        inputFile = fopen( "input.txt", "r");



    if( inputFile == NULL )
    {
        printf( "Error: File could not be opened" );
        /*report failure*/
        return 1;
    }   
    i = 0;
    size = 0;
    while( fscanf( inputFile, "%s", temp) == 1 )
    {
        exists = 0;
        for( j = 0; j < size; j++ )
        {
            if( strcmp( arrayOfWords[j].word, temp ) == 0 )
            {
                arrayOfWords[j].count++;
                exists = 1;
            }
        }
        if( exists == 0 )
        {
            arrayOfWords[i].word = malloc(sizeof(char) 
                                    * (strlen(temp)+1));
            strcpy( arrayOfWords[i].word, temp );
            /*arrayOfWords[i].count++;*/
            size++;
        }
        i++;
    }

    for( i = 0; i < (size-1) ; i++ )
        printf("%s\n", arrayOfWords[i].word);


    fclose( inputFile );
    /*for( i = 0; i < size-1; i++ )
        free( arrayOfWords[0].word );*/
    return 0; 
}   
定义GNU源
#包括
#包括
#包括
typedef无符号整数单元;
类型定义结构字类型
{
字符*字;
单位计数;
};
/*将该字符串存储在另一个临时字符串中,您可以对其进行迭代
并与非重复单词数组进行比较。如果有,增量*/
内部主(空)
{
typedef struct wordType*WORD_记录;
WORD_-RECORD arrayOfWords=malloc(10*sizeof(WORD_-RECORD));
文件*输入文件;
煤焦温度[50];
uint i;
uint j;
单位尺寸;
uint存在;
inputFile=fopen(“input.txt”、“r”);
if(inputFile==NULL)
{
printf(“错误:无法打开文件”);
/*报告失败*/
返回1;
}   
i=0;
尺寸=0;
而(fscanf(输入文件,“%s”,temp)==1)
{
存在=0;
对于(j=0;j
您似乎正确地使用了
malloc()
来初始化您的ArrayOfWord数组。您可以使用
realloc()
函数来扩展数组,但是您必须跟踪数组的大小以及有多少个单词,以便知道何时调用
realloc()
。在
if(exists==0)
的情况下,变量
arrayOfWords[i].count
尚未初始化,因此假设它为零是一个bug,尝试递增它是一个bug,而除了将它设置为显式值(在本例中,
0
)之外的任何操作都是bug


您似乎在使用
i
来计算您阅读的单词总数,而不是您阅读的唯一单词,因此在打印单词时使用
i
作为循环计数器也是错误的。当您释放
malloc()
的单词时,情况也是如此:使用
i
作为循环计数器意味着您最终将
free()
malloc()
获得您没有得到的东西

要动态增加单词列表的存储空间,您需要跟踪已分配存储空间的
struct wordType
项的数量,并在添加新词时检查是否已达到限制,必要时检查
realloc()


当循环打印(并释放)单词时,为什么要执行“大小-1”?

是否有生成错误?我有一个很好的感觉,你应该使用
->
去引用
单词和
计数
变量,因为你正在存储结构指针。如果
->
符号不正确,word\u RECORD可能是一个指针,但他使用malloc()为十个结构分配存储,点符号是合适的。使用typedef没什么错,你说的“阻止封装”是什么意思?calloc()是分配存储的一种可行的替代方法,但在我看来,它唯一的优点是初始化分配的存储,而提问者在提问代码中忘记了这样做。