简体   繁体   中英

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. 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];

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).

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

  • 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.

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".

SUGGESTION:

1) Boil your code down to a simple, standalone example

2) Run it in the debugger

3) When it "terminates unexpectedly", use the debugger to get a "stack traceback" - you must identify the specific line that's failing

4) You should also look for a specific error message (if possible)

5) Post your code, the error message and your traceback

6) Be sure to tell us what platform (eg Centos Linux 5.5) and compiler (eg gcc 4.2.1) you're using, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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