The problem can be simply a limited amount of storage available to your
program during its execution. That's beyond the language specification,
you need to consult the manual for programming your OS and/or the manual
for your compiler.
Often different sizes of arrays can be had for static, automatic, and
dynamic arrays. Since you didn't indicate what kind of an array you had,
I assume it was automatic. Look for a compiler switch that allows you
to control the "stack size" (which is not related to std::stack template).
But please don't ask us, particular compiler switches are off-topic here.
Victor
"eggie2486" <eggy...@twcny.rr.com> 写入邮件
news:hzZhc.77165$M3....@twister.nyroc.rr.com...
I think it is related to the size of the stack allocated by the
compiler/linker. I think 4KB might be the standard stack size for a x86.
I sure you will find some switch for the compiler/linker you are using
to change the default stack size.
> It depends on your pc's memory size.
> for example,if your declared an array: int array[3001][3001]
> it needs 32bit*3001*3001=288M memory space.
> in such way,you can calculate the Maximum size of array you can you use
> according
> to your array type and memory size.
Just to nitpick:
1. Don't top-post.
2. Your program requires: sizeof(int) * 3001 * 3001, if this was
a true "square" area of memory. The big issue is that
sizeof(int) may not be 32 bits on all platforms. Some have
16 bit ints, others have 64 or larger.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
No, has nothing to do with the stack area, but the area where the
array is allocated from. The OP did not say whether the array was
created from dynamic memory (a.k.a the heap), block or local memory
(a.k.a. the stack), or where automatic variables are declared.
The amount of memory available depends on:
1. The total memory on the platform.
2. The amount of memory allocated to the program.
3. The compiler's settings.
32bit*3001*3001 is not 288M, it's approx 36M.
Plus sizeof(int) is not necessarily 32 bits.
- J.
That's all true. But if sizeof(int)*CHAR_BIT is not 32 (but
smaller, supposedly), then sizeof(int*) is smaller too, probably.
Then for such system it's rather difficult to organise addressing
in an array whose size is greater than 2^(sizeof(int*)*CHAR_BIT).
For example, on 16-bit systems it was a chore to have an array
larger than 64K in total bytes, no matter how much memory your
computer had installed.
Victor
> That's all true. But if sizeof(int)*CHAR_BIT is not 32 (but
> smaller, supposedly), then sizeof(int*) is smaller too, probably.
Why would you think that? There's no relationship between the size of
int and the size of pointers despite all the code out there (much of it
promoted as "portable") that assumes sizeof(int)==sizeof(whatever*).
Because I worked on more than one platform where that was true.
I also worked on a platform where it wasn't true, but you had to
take special steps.
> There's no relationship between the size of
> int and the size of pointers despite all the code out there (much of it
> promoted as "portable") that assumes sizeof(int)==sizeof(whatever*).
I am not aware of any such code, but you must be right, you state
all that with such conviction... Besides, I did say "probably",
didn't I?
Allocate from the heap:
typedef int (*array_t)[3001];
array_t array = new array_t[3001];
or, more likely,
class MagicSquare
{
//...
private:
int m_squares[3001][3001];
};
MagicSquare* magicSquare = new MagicSquare(); //allocate from heap
Generally, when allocating large amounts of memory, use dynamic
storage. Automatic storage is generally much more limited than dynamic
storage.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
>On Fri, 23 Apr 2004 00:37:33 GMT, "eggie2486" <eggy...@twcny.rr.com>
>wrote:
>
>>What is the maximum size of an array? I tried to edit an extremely large
>>array for a magic square, for example, array[3001][3001], and when I ran the
>>program, it would not display the array. I changed the array to 105 by 105
>>and it worked fine. Does anybody know what the problem is and how I could
>>possibly get around it?
>
>Allocate from the heap:
>
>typedef int (*array_t)[3001];
>array_t array = new array_t[3001];
I messaged that up!
typedef int array_t[3001];
array_t* array = new array_t[3001];
then you can do array[50][400], etc.