Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 211
user1843701 Avatar asked Nov 22 '12 01:11

user1843701


People also ask

What is max size of 2D array in C++?

The maximum allowable array size is 65,536 bytes (64K). Reduce the array size to 65,536 bytes or less.

What is the maximum size of 2D array in Java?

A Java program can only allocate an array up to a certain size. It generally depends on the JVM that we're using and the platform. Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.

How do you declare and initialize 1 D 2D array with an example?

Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.

How many dimensions can you have in an array?

Although an array can have as many as 32 dimensions, it is rare to have more than three. When you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.


1 Answers

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.

like image 151
Sergey Kalinichenko Avatar answered Nov 15 '22 04:11

Sergey Kalinichenko