简体   繁体   English

为什么248x248是我可以声明的最大二维数组大小?

[英]Why is 248x248 the maximum bi dimensional array size I can declare?

I have a program problem for which I would like to declare a 256x256 array in C. Unfortunately, I each time I try to even declare an array of that size (integers) and I run my program, it terminates unexpectedly. 我有一个程序问题,我想在C中声明一个256x256数组。不幸的是,我每次尝试甚至声明一个大小的数组(整数)并运行我的程序时,它意外终止。 Any suggestions? 有什么建议? I haven't tried memory allocation since I cannot seem to understand how it works with multi-dimensional arrays (feel free to guide me through it though I am new to C). 我没有尝试过内存分配,因为我似乎无法理解它如何与多维数组一起工作(尽管我是C的新手,随时引导我完成它)。 Another interesting thing to note is that I can declare a 248x248 array in C without any problems, but no larger. 另一个有趣的事情是我可以在C中声​​明一个248x248阵列没有任何问题,但没有更大的问题。

dims = 256;  
int majormatrix[dims][dims];

Compiled with: 编译:

gcc -msse2 -O3 -march=pentium4 -malign-double -funroll-loops -pipe -fomit-frame-pointer -W -Wall -o "SkyFall.exe" "SkyFall.c"

I am using SciTE 323 (not sure how to check GCC version). 我使用的是SciTE 323(不知道如何检查GCC版本)。

There are three places where you can allocate an array in C: 在C中有三个可以分配数组的地方:

  • In the automatic memory (commonly referred to as "on the stack") 自动存储器中(通常称为“在堆栈上”)
  • In the dynamic memory ( malloc / free ), or 动态内存( malloc / free )中,或者
  • In the static memory ( static keyword / global space). 静态内存( static关键字/全局空间)中。

Only the automatic memory has somewhat severe constraints on the amount of allocation (that is, in addition to the limits set by the operating system); 只有自动内存对分配量有一些严格的限制(即,除了操作系统设置的限制外); dynamic and static allocations could potentially grab nearly as much space as is made available to your process by the operating system. 动态和静态分配可能会占用操作系统为您的进程提供的空间。

The simplest way to see if this is the case is to move the declaration outside your function. 查看是否是这种情况的最简单方法是将声明移到函数之外。 This would move your array to static memory. 这会将您的数组移动到静态内存。 If crashes continue, they have nothing to do with the size of your array. 如果崩溃继续,则它们与阵列的大小无关。

Unless you're running a very old machine/compiler, there's no reason that should be too large. 除非您运行的是非常旧的机器/编译器,否则没有理由应该太大。 It seems to me the problem is elsewhere. 在我看来,问题出在其他地方。 Try the following code and tell me if it works: 尝试以下代码并告诉我它是否有效:

#include <stdio.h>

int main()
{
  int ints[256][256], i, j;
  i = j = 0;
  while (i<256) {
    while (j<256) {
    ints[i][j] = i*j;
    j++;
   }
   i++;
   j = 0;
 } 
 printf("Made it :) \n");
 return 0;
}

You can't necessarily assume that "terminates unexpectedly" is necessarily directly because of "declaring a 256x256 array". 您不一定会认为“意外终止”必然是因为“声明256x256阵列”。

SUGGESTION: 建议:

1) Boil your code down to a simple, standalone example 1)将您的代码简化为一个简单的独立示例

2) Run it in the debugger 2)在调试器中运行它

3) When it "terminates unexpectedly", use the debugger to get a "stack traceback" - you must identify the specific line that's failing 3)当它“意外终止”时,使用调试器获得“堆栈追溯” - 您必须确定失败的特定行

4) You should also look for a specific error message (if possible) 4)您还应该查找特定的错误消息(如果可能)

5) Post your code, the error message and your traceback 5)发布您的代码,错误消息和您的追溯

6) Be sure to tell us what platform (eg Centos Linux 5.5) and compiler (eg gcc 4.2.1) you're using, too. 6)一定要告诉我们你正在使用的平台(例如Centos Linux 5.5)和编译器(例如gcc 4.2.1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM