5

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

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
user1843701
  • 51
  • 1
  • 3
  • You should be able to do so, unless you are using an old compiler (?!) (I tested this on Windows, which has stack size of 1MB). – nhahtdh Nov 22 '12 at 01:51
  • 2
    You may be blowing up your stack by declaring it locally. That in itself is not a huge array for most machines. There are a multitude of answers on SO about how to declare and use a dynamicly allocated 2-d array. Search a bit. – Duck Nov 22 '12 at 01:56
  • Can you post a code sample of how you're initializing the array? – Pete Baughman Nov 22 '12 at 01:56
  • i am using a 8GB, 64-bit windows 7 ultimate machine with an i7. – user1843701 Nov 22 '12 at 02:00
  • 1
    How exactly are you defining `dims = 256`? Is that a `#define` or an `enum` or a `const int` or just an `int`? In either of the latter two cases, you're using a VLA — variable length array. That's a C99 feature, so I assume that you're not using it (unless you're also not using MSVC). Please update the question with the accurate information (rather than adding comments). The array occupies 256 KiB; that's a moderately large array for placing on the stack; it is not overly large for a statically or dynamically allocated array. – Jonathan Leffler Nov 22 '12 at 02:05
  • `gcc --version` gives you the version information. GCC supports C99 and will allow VLAs. – Jonathan Leffler Nov 22 '12 at 02:09
  • Ok, I am using GCC 3.4.2. Sorry about the comments, took me a minute to get use to the live posts. Currently, my arrays are defined by just an int. – user1843701 Nov 22 '12 at 02:35
  • What is the error message? – Ciro Santilli OurBigBook.com Aug 06 '15 at 18:52

3 Answers3

10

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

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;
}
Ricky Stewart
  • 1,102
  • 1
  • 8
  • 18
  • Again - the OP should run a standalone test (like your example) under the debugger, determine exactly where it's crashing, and post back 1) the full error message, 2) the point of failure, and 3) compiler and platform. IMHO... – paulsm4 Nov 22 '12 at 02:05
  • Yes, your code does work. Thanks for your help! Now my problem might lie elsewhere but could it be the fact that I am declaring a lot of these large matrices (say, less than ten)? In my original code (which is too long to post I think) I have a lot of loops, nested loops and 256x256 matrices rolling around everywhere... – user1843701 Nov 22 '12 at 02:11
  • That could certainly be the problem. Try dynamically allocating those arrays and see if that fixes it. Paul is correct, though -- the most surefire solution would be to backtrace the error with a debugger. – Ricky Stewart Nov 22 '12 at 02:16
1

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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • @user1843701 - thank you for the additional info. Try this: `int majormatrix[256][256];`. As Jonathan Leffler correctly pointed out, perhaps you're unintentially invoking a VLA (`int majormatrix[x][y]`), whereas all you *really* wanted was a fixed-length array. Try a constant instead of a variable, and see if it helps. IMHO... – paulsm4 Nov 22 '12 at 02:12
  • Hate to ask but how do I run my code in the debugger? I am using SciTE, if that is at all helpful. – user1843701 Nov 22 '12 at 02:37
  • SciTE is a text editor. It's part of [Scintilla](http://www.scintilla.org/). I confess - I'm not familiar with either one. But it sounds like SciTE/Scintilla use gcc on your system. And gcc works well with the gdb debugger. Here's one (of many) good "quick-start guides" on gdb: http://www.cs.cmu.edu/~gilpin/tutorial/ – paulsm4 Nov 22 '12 at 04:37
  • And based on your comments below, it sounds like changing variable "dims" to constant "256" in your array declaration resolved the problem. Cool :) – paulsm4 Nov 22 '12 at 04:38