Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

c programming more efficient use of big arrays

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...