How would I make these big arrays more efficient? I am getting a segmentation fault when I add them, but when I remove them the segmentation fault goes away. I have several big arrays like this that are not shown. I need the arrays to be this big to handle the files that I am reading from. In the code below I used stdin instead of the file pointer I would normally use. I also free each big array after use.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void) {
int players_column_counter = 0;
int players_column1[100000] = {0};
char *players_strings_line_column2[100000] = {0};
char *players_strings_line_column3[100000] = {0};
char *players_strings_line_column4[100000] = {0};
char *players_strings_line_column5[100000] = {0};
char *players_strings_line_column6[100000] = {0};
char line[80] = {0};
while(fgets(line, 80, stdin) != NULL)
{
players_strings_line_column2[players_column_counter] =
malloc(strlen("string")+1);
strcpy(players_strings_line_column2[players_column_counter],
"string");
players_column_counter++;
}
free(*players_strings_line_column2);
free(*players_strings_line_column3);
free(*players_strings_line_column4);
free(*players_strings_line_column5);
free(*players_strings_line_column6);
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…