Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
237 views
Welcome To Ask or Share your Answers For Others

1 Answer

Read much more about C dynamic memory allocation. Learn to use malloc and calloc and free. Notice that malloc and calloc (and realloc) can fail, and you need to handle that. See this and that.

The call stack is limited in size (to one or a few megabytes typically; the actual limit is operating system and computer specific). It is unreasonable to have a call frame of more than a few kilobytes. But calloc or malloc might permit allocation of a few gigabytes (the actual limit depends upon your system), or at least hundreds of megabytes on current laptops or desktops. A local -automatic variable- array of more than a few hundreds elements is almost always wrong (and is surely very bad smell).

BTW, if your system has getline(3), you probably should want to use it (like here). And likewise for strdup(3) and asprintf(3).

If your system don't have getline, or strdup, or asprintf, you should consider implementing them, or borrow some free software implementation of them.

Compile with all warnings and debug info (e.g. gcc -Wall -Wextra -g with GCC). Improve your code to get no warnings. Use the debugger gdb (and valgrind). Beware of undefined behavior (such as buffer overflows) and of memory leaks.

Study the source code of existing free software (e.g. on github and/or some Linux distribution) for inspiration.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...