Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Maximum size of an array

7 views
Skip to first unread message

eggie2486

unread,
Apr 22, 2004, 8:37:33 PM4/22/04
to
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?


Victor Bazarov

unread,
Apr 22, 2004, 8:57:24 PM4/22/04
to
"eggie2486" <eggy...@twcny.rr.com> wrote...

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


Eric Lee

unread,
Apr 22, 2004, 9:01:38 PM4/22/04
to
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.

"eggie2486" <eggy...@twcny.rr.com> 写入邮件
news:hzZhc.77165$M3....@twister.nyroc.rr.com...

.

unread,
Apr 22, 2004, 9:03:48 PM4/22/04
to

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.

Thomas Matthews

unread,
Apr 22, 2004, 10:23:08 PM4/22/04
to
Eric Lee wrote:

> 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

Thomas Matthews

unread,
Apr 22, 2004, 10:26:18 PM4/22/04
to
. wrote:

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.

Jacek Dziedzic

unread,
Apr 23, 2004, 5:47:57 AM4/23/04
to
Eric Lee wrote:
> 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.

32bit*3001*3001 is not 288M, it's approx 36M.

Plus sizeof(int) is not necessarily 32 bits.

- J.

Victor Bazarov

unread,
Apr 23, 2004, 9:16:14 PM4/23/04
to
"Jacek Dziedzic" <jacek__...@janowo.net> wrote...

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


Bill Seurer

unread,
Apr 26, 2004, 1:03:25 PM4/26/04
to
Victor Bazarov wrote:

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

Victor Bazarov

unread,
Apr 27, 2004, 9:02:18 PM4/27/04
to
"Bill Seurer" <seu...@us.ibm.com> wrote...

> Victor Bazarov wrote:
>
> > 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?

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?


tom_usenet

unread,
Apr 28, 2004, 4:44:59 AM4/28/04
to
On Fri, 23 Apr 2004 00:37:33 GMT, "eggie2486" <eggy...@twcny.rr.com>
wrote:

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

tom_usenet

unread,
Apr 28, 2004, 9:43:32 AM4/28/04
to
On Wed, 28 Apr 2004 09:44:59 +0100, tom_usenet
<tom_u...@hotmail.com> wrote:

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

0 new messages