Reputation: 439
I am coding a program which takes a text file as an input, makes the index of the words of it and prints the output(the index) in a file and in the screen.
the input file may be huge. but we KNOW that the maximum variety of the words used in the text file is 200. we don't know what's the maximum of lines and characters of each word. so I should reserve a large number for them. I took the maximum of line 1000 and the maximum characters of each word 100.
I am programming in Turbo C and (I am forced to use that). the compiler allocates just 64kb memory (with the size of the compiler included) and so I have to use MALLOC.
my program is supposed to work in this algorithm:
it reads the input file line by line with fgets. then in the current line, it reads word by word with strtok. so far I have the xth word in yth line. I want to put the words in an array of pointers. so I need a char * word[200]
. and I want to show how many times, which word is repeated in which line. so I need a int index [200][1000]
. if in yth line, the xth word is existed I would do index[x][y]++
.
so now I need to allocate MALLOC memory to these char * word[200]
and int index[200][1000]
. Can anyone help? I tried all the answers to these question and none of them helped.
Upvotes: 1
Views: 405
Reputation: 7620
char * words = malloc(200*100);
int * index = malloc(200*1000*sizeof(int));
// word[i*200+j] : character j in word i
// index[i*200+j] : int at index i,j
alternatives:
// mallocing an array for storing a maximum of 200 malloced words
char ** words = malloc(200*sizeof(char*));
// adding a new word, at index i, which is pointed to by pszNewWord (null terminated)
words[i] = strdup(pszNewWord);
Upvotes: 0
Reputation: 1979
If I understand you
In the first loop, I want to define an array of 200 pointers that each pointer, points to an array of char blocks. I want each pointer, points to an array of maximum 100 bytes. Meaning 100 char blocks
char **words = NULL;
int i;
words = malloc(sizeof(char*) * 200);
for(i = 0; i < 200; i++) {
words[i] = malloc(100);
}
There's allocating 200 words
with 100 bytes size here.
In the second loop, I want to define a 2D array of int blocks that each block is maximum 2 bytes. meaning 200 * 1000 int blocks.
int **index = NULL;
int i;
index = malloc(sizeof(int*) * 200)
for (i = 0; i < 200; i++) {
index[i] = malloc(sizeof(int) * 1000);
}
Here you allocate 200x1000 int
array.
Upvotes: 1
Reputation: 748
You don't quite have malloc right. Your malloc(100) is only allocating 100 bytes. You need
char * words[i] = malloc(sizeof(char *) * 100);
This allocates 800 bytes (100 elements of 8 bytes (size of a pointer) each).
Similarly, in the second malloc, you want two integers, you need
int index[i][j] = malloc(sizeof(int *) * 2);
You shouldn't cast to a pointer; it returns a void pointer, which is implicitly cast to whatever type of pointer you need just by virtue of the assignment.
http://www.cplusplus.com/reference/cstdlib/malloc/
FURTHERMORE:
Additionally, you're trying to stuff 2 bytes into an integer pointer or 4 bytes (100-96 = 4; the 96 is 8 * 12) into a character pointer. I have no idea in the world what that will do. The BEST you can hope for is that you'll just lose the memory somewhere and effectively have 12 character pointers and 2 memory leaks.
Upvotes: 2