C malloc problem, been stuck forever!
#include <stdio.h> #include <stdlib.h> typedef struct { char word; int freq; } WORDSTRUCT; int main(int argc, char *argv[]){ int i = 0; WORDSTRUCT *words_array; words_array = (WORDSTRUCT *) malloc (sizeof(WORDSTRUCT)); FILE *theFile; theFile = fopen("data.txt", "r"); while (fscanf(theFile, "%s", &words_array[i].word) == 1){ printf("%s \n", &words_array[i].word); i++; } return 0; }
So what I am trying to do is take a text file containing words, and word by word insert it into a (dynamic) array. For some reason, it only does the first 5 and then it stops
You allocation memory for an array, but for how many array elements? You only allocate for a single WORDSTRUCT. You'll need to figure out how large your array will be before you use malloc. You can for example loop through the file once to count the number of words, then malloc the array and only then read the array. Or you can use a maximum array size (but what's the point of dynamic memory then?). Some other remarks related to this: if you allocate dynamic memory, be sure to free it (calling a function called `free`) when you no longer use it. While your program will work even if you do not free it, the available memory will reduce each time you run your program (it's called memory leaking. It slows down your computer). Another (possible) issue is that your struct will only contain room for a single character, but you're reading an entire string into it. You should either make the char a fixed-size array (what's the largest word you're expecting?) or do some more dynamic memory allocation.
Thanks for your response slotema, from what I understood the use of dynamic arrays is when you don't know the size of the array, so it dynamically adjusts its size as I add more elements. If I went and looped through the file to count the number of words and then free up memory according to how many words I counted, what's the purpose of using malloc and dynamic arrays?
The purpose of dynamic memory is to create (e.g.) arrays of which you do not know the size of an array when you're compiling a program. In your example, you don't know whether the file contains 2 words, or 20 words or even 1 000 words. If you declared your array as `char *words[2]` and your file contains 20 words, that's a problem. But if you write your program in such a way that it can determine the number of words (or at least the maximum number of words), you can use that number to create an array. But how can you determine the number of words? You could count all the words in the file before reading them, or pass the number of words as an argument or inside the file (e.g. the first line can contain a number indicating the number of words), or you can do a rough estimation. E.g. half of the filesize is a maximum for the number of words. That would assume each word is one character, followed by a single whitespace.
Thank you slotema, now I understand!
Join our real-time social learning platform and learn together with your friends!