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

后端 未结 3 1606
难免孤独
难免孤独 2020-12-02 00:48

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

相关标签:
3条回答
  • 2020-12-02 01:22

    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.

    0 讨论(0)
  • 2020-12-02 01:24

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 01:34

    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 (e.g. Centos Linux 5.5) and compiler (e.g. gcc 4.2.1) you're using, too.

    0 讨论(0)
提交回复
热议问题