c编程更有效地使用大数组 [英] c programming more efficient use of big arrays

查看:83
本文介绍了c编程更有效地使用大数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使这些大型阵列更有效率?添加它们时出现分段错误,但是删除它们时,分段错误消失了。我有几个这样的大数组未显示。我需要这么大的数组来处理我正在读取的文件。在下面的代码中,我使用stdin代替了通常使用的文件指针。使用后,我还释放了每个大数组。

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;
}


推荐答案

了解有关 C动态内存分配。学会使用 malloc calloc 免费 。请注意, malloc calloc (和 realloc )可能会失败,您需要处理。请参阅 a>。

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.

调用堆栈的大小受到限制(通常限制为一兆或几兆字节;实际限制是特定于操作系统和计算机的)。呼叫帧超过几千字节是不合理的。但是 calloc malloc 可能允许分配几GB的字节数(实际限制取决于您的系统),或者至少当前笔记本电脑或台式机上的数百兆字节。包含数百个元素的局部-自动变量-数组几乎总是错误的(而且肯定是非常难闻的气味。)

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,如果您的系统具有 getline(3),您可能应该使用它(例如这里)。同样对于 strdup(3) asprintf(3)

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

如果您的系统没有 getline strdup asprintf ,则应考虑实施它们,或借用它们的一些免费软件实现。

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

编译所有警告和调试信息(例如 gcc -Wall -Wextra -g GCC )。改进您的代码,不发出任何警告。 使用调试器 gdb (以及 valgrind )。提防未定义的行为(例如缓冲区溢出 s)和内存

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.

研究现有免费软件(例如在 github 和/或某些 Linux发行版)以获取灵感。

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

这篇关于c编程更有效地使用大数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆