-2

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;
}
  • 3
    Use `malloc` (and friends) to allocate dynamically. – P.P Oct 28 '17 at 19:51
  • The segmentation fault is most likely due to a Stack Overflow (get the joke?), which is when memory is located over the bounds of a special, small, and fast section of memory reserved for the most important parts of your program. Use malloc to correct this, as @usr said above. – Cpp plus 1 Oct 28 '17 at 19:55
  • 1
    Free for automatic variables? Interesting idea. You need a C book – 0___________ Oct 28 '17 at 19:55
  • @Cppplus1 like that? – noobprogrammer1987 Oct 28 '17 at 19:59
  • @noobprogrammer1987 Yes, the malloc part is OK. But your program as a whole still doesn't make much sense. See the duplicate for some ideas and you can obviously google for more examples. – P.P Oct 28 '17 at 19:59
  • 1
    You really need to research how to `free` an array of pointers. Generally, you should be calling `free` as many times as you called `malloc` (i.e., you need a loop). `free(*players_strings_line_column2);` will just free the first element of that array, but not the rest. – Nik Oct 28 '17 at 20:07

1 Answers1

0

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547