C中允许的最大静态数组大小是多少? [英] What maximum size of static arrays are allowed in C?

查看:1127
本文介绍了C中允许的最大静态数组大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的算法中,我知道使用静态数组,而不使用动态数组.但是我有时候 达到堆栈的极限.我是对的,那些静态数组存储在堆栈中了吗?

In my algorithm I know work with static arrays, no dynamic ones. But I sometimes reach the limit of the stack. Am I right, that static arrays are stored to the stack?

哪个参数会影响一个C程序的最大堆栈大小?

Which parameters affect my maximum stack size for one C programm?

是否有许多系统参数会影响最大数组大小?请问最大号没有.的元素取决于数组类型?它取决于总系统RAM吗?还是每个C程序都具有静态的最大堆栈大小?

Are there many system parameters which affect the maximal array size? Does the maximunm no. of elements depend of the array type? Does it depend on the total system RAM? Or does every C programm have a static maximum stack size?

推荐答案

您的大多数问题都已得到回答,但只是给出一个使我的生活更加轻松的答案:

Most of your questions have been answered, but just to give an answer that made my life a lot easier:

定性,非动态分配的阵列的最大大小取决于您拥有的RAM数量.它还取决于数组的类型,例如int可能是4个字节,而double可能是8个字节(它们也与系统有关),因此,如果使用int而不是double,则您将能够拥有一个数组,其元素数量为两倍

Qualitatively the maximum size of the non-dynamically allocated array depends on the amount of RAM that you have. Also it depends on the type of the array, e.g. an int may be 4 bytes while a double may be 8 bytes (they are also system dependent), thus you will be able to have an array that is double in number of elements if you use int instead of double.

尽管如此,但要记住有时数字确实很重要,这是一个非常笨拙的代码段,可帮助您提取系统中的最大数量.

Having said that and keeping in mind that sometimes numbers are indeed important, here is a very noobish code snippet to help you extract the maximum number in your system.

#include <stdio.h>
#include <stdlib.h>

#define UPPER_LIMIT 10000000000000 // a very big number

int main (int argc, const char * argv[])
{
    long int_size = sizeof(int);
    for (int i = 1; i < UPPER_LIMIT; i++)
    {
        int c[i];
        for (int j = 0; j < i; j++)
        {
            c[j] = j;
        }
        printf("You can set the array size at %d, which means %ld bytes. \n", c[i-1], int_size*c[i-1]);
    }    
}

PS:可能需要一段时间才能达到系统的最大值,并产生预期的分段错误,因此您可能需要将i的初始值更改为更接近系统RAM的值,以字节为单位.

P.S.: It may take a while until you reach your system's maximum and produce the expected Segmentation Fault, so you may want to change the initial value of i to something closer to your system's RAM, expressed in bytes.

这篇关于C中允许的最大静态数组大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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