469,643 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

Maximum size of an array

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?
Jul 22 '05 #1
11 24810
"eggie2486" <eg******@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?


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
Jul 22 '05 #2
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" <eg******@twcny.rr.com> дʼ
news:hz****************@twister.nyroc.rr.com...
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?

Jul 22 '05 #3
.
eggie2486 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?


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.
Jul 22 '05 #4
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

Jul 22 '05 #5
.. wrote:
eggie2486 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?


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.


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

Jul 22 '05 #6
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.
Jul 22 '05 #7
"Jacek Dziedzic" <ja*************@janowo.net> wrote...
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.


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
Jul 22 '05 #8
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*).
Jul 22 '05 #9
"Bill Seurer" <se****@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?
Jul 22 '05 #10
On Fri, 23 Apr 2004 00:37:33 GMT, "eggie2486" <eg******@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];

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
Jul 22 '05 #11
On Wed, 28 Apr 2004 09:44:59 +0100, tom_usenet
<to********@hotmail.com> wrote:
On Fri, 23 Apr 2004 00:37:33 GMT, "eggie2486" <eg******@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.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by chotiwallah | last post: by
7 posts views Thread by Joey C. | last post: by
2 posts views Thread by Kums | last post: by
6 posts views Thread by Divick | last post: by
23 posts views Thread by Gerrit | last post: by
7 posts views Thread by tomlebold | last post: by
2 posts views Thread by Woody Ling | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.