c++: array size limit?
Hello:
I'm just trying to make a 2 dimensional integer array with the following code but keep getting a segmentation fault... I'm running on mandrake 10.1 and gcc 3.4.1. ============================= #include <iostream.h> int main() { int i[2000][2000]; int j,k; for (j=0; j<2000; j++) { for (k=0; k<2000; k++) { i[j][k] = j+k; } } return 0; } ============================= Works okay with smaller arrays (e.g. 1000x1000)... So is there a size limit on the size of arrays? If so, how can I change it? If not, any suggestions on how I should approach? Thank you very much... dud |
My guess is that the maximum size of an array is 4.2 billion (2^32) bytes, as an array position (for 1-d array) is calculated in the processor by:
Code:
base+index*size Code:
base+rowindex*colsize+colindex*size Code:
numrows*numcols*sizeof(element)<(2^32) Your 2000x2000 is only hitting 16,000,000 (2000*2000*4 bytes). The only other thing I can imagine is some sort of limitation I am not thinking of... such as declaring it statically. |
first off, I'd use a dynamically allocated array. The static array will make your binary bloated like crazy (if an int is defined as 32 bits, you have 128000000 bits allocated by the compiler, meaning 16,000,000 bytes. That's a BIG binary for such small code)
int **i = new int[2000][2000]; or something like that ( I'm somewhat hazy on how to do a multi-dimensional array in C++) Also, I think that you're trying to addres a memory region that's way to large (think of 2000 * 2000, you get 4000000 byte address. This is more than most systems can address if my math is correct, which it probably isn't as I'm doing this back-of-the-envelope.) Therefore, yes there's a limitation, but it's hardware related not software related. -Aaron |
The size of an array does nothing to the size of the executable. However, an array that large is simply too big to be allocated on the stack on most systems. Allocate it dynamically or, probably better, use a container from the C++ standard library that handles the dynamic allocation for you. A std::vector for example.
And btw, don't include iostream.h, include iostream. iostream.h is not part of standard C++. |
Put your array in the static area by prefixing it with the static keyword:
Code:
static int i[2000][2000]; Be aware that your function is no more reentrant after that. |
Best of all, do:
Code:
int **array=malloc(2000*2000*sizeof(int)); (for those who say malloc is not C++ enough, that may be true, but it's still functional) |
Don't use malloc()/free() in C++, it doesn't know about constructors and destructors. For more details, read Meyers.
|
malloc/free is fine if you're not doing it to objects. and what is Meyers?
|
malloc and free is fine in C, in C++ use new and delete.
Scott Meyers has written very goo C++ books, the "Effective C++"-series. |
Thanks a bunch for all the help.
Cheers to all. Dud |
I learn recently an way to create/destroy dynamic array using new/delete, as example:
// creating an int NxN array int N = 1000; int **p; p = new int*[N]; for( int i = 0; i < N; i++ ) { p[i] = new int[N]; for( int j = 0; j < N; j++ ) p[i][j] = 0; } ... // destoying the array for( int i = 0; i < N; i++ ) delete [] p[i]; delete [] p; |
Does:
Code:
int **array=new int[1000][1000]; |
Short answer: Probably no (depends on what you expect, though).
the problem is that with this code, array[x][y] cannot be properly computed, because int** has insufficient information about array sizes. What happens under water is that to find the proper memory address of array[x][y], the compiler computes something like Code:
x*sizeof(array[0]) + y*sizeof(array[0][0]) Groetjes, Kees-Jan |
Not only that, you will also get a compiler error.
error: cannot convert `int (*)[1000]' to `int**' in initialization To create a multidimensional array dynamically, you have to first initialize the pointer array, then initialize memory for each pointer in that array. This isn't quite the same as a static multidemensional array because it is not likely to be a contiguous block of memory. Another option is to just initialize a contiguous block of memory for a single dimension array and index it appropriately. (e.g. index = x + y*width; ) |
There are probably stack size limits enforced. SIGSEGV is sent when those limits are turned on and you blow past them. You can check to see with something like this:
Code:
#include <sys/time.h> |
All times are GMT -5. The time now is 01:18 PM. |