为什么 248x248 是我可以声明的最大二维数组大小? [英] Why is 248x248 the maximum bi dimensional array size I can declare?

查看:14
本文介绍了为什么 248x248 是我可以声明的最大二维数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序问题,我想在 C 中声明一个 256x256 的数组.不幸的是,每次我尝试声明一个该大小(整数)的数组并运行我的程序时,它意外终止.有什么建议?我没有尝试过内存分配,因为我似乎无法理解它是如何处理多维数组的(尽管我是 C 新手,但请随时指导我完成它).另一个值得注意的有趣的事情是,我可以在 C 中声明一个 248x248 的数组,没有任何问题,但不能更大.

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. 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). Another interesting thing to note is that I can declare a 248x248 array in C without any problems, but no larger.

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

编译:

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

我正在使用 SciTE 323(不确定如何检查 GCC 版本).

I am using SciTE 323 (not sure how to check GCC version).

推荐答案

C 中有三个地方可以分配数组:

There are three places where you can allocate an array in C:

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

只有自动内存对分配量有一些严格的限制(即除了操作系统设置的限制);动态和静态分配可能会占用几乎与操作系统为您的进程提供的空间一样多的空间.

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.

这篇关于为什么 248x248 是我可以声明的最大二维数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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