Board index » delphi » Maximum Size Of An Array?

Maximum Size Of An Array?

#include <iostream>
int main() {
 std::cout << "Here\n";
 int x[300000];
 std::cout << "Hello, World!\n";

Quote
}

This compiles ok but the program quits straight away when I run it and
doesn't output anything.  However, if I change the array declaration to int
x[200000], it compiles and runs ok.  I am running XP with 512Mb RAM.

The funny thing is if I replace int x[300000] with int *x=new int[300000]
which uses the same amount of memory, it works ok too. So why doesn't the
above program work when I easily have enough memory?

Gaz

 

Re:Maximum Size Of An Array?


Quote
On Tue, 25 Jun 2002 19:15:26 +0100, "Gaz" <garyj...@hotmail.com> wrote:
>int main() {
> std::cout << "Here\n";
> int x[300000];
>This compiles ok but the program quits straight away when I run it

Using which compiler and which version?
Creating what kind of program? DOS? Win16? Win32?

Quote
>if I change the array declaration to int
>x[200000], it compiles and runs ok.  

You are creating a local non-static variable which will be created on
the stack. Assuming it's a Win32 console program, where ints are
4-bytes long, your 300000 int array will be 1,200,000 bytes. That's
a hefty chunk of the stack, which is likely 2MB by default. You are
probably exhausting the stack and getting stack overrun. Changing
it to 200000 means you're "only" using 800,000 bytes.

Note that when testing you should always build with the option enabled
to check for stack overflow.

Quote
>The funny thing is if I replace int x[300000] with int *x=new int[300000]
>which uses the same amount of memory, it works ok too.

This is the correct way to create large arrays. They will be created
on the heap/store which in Win32 will have access to all of virtual
memory, making around 2GB available for your program to use.

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:Maximum Size Of An Array?


Quote
> Using which compiler and which version?

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

Quote
> Creating what kind of program? DOS? Win16? Win32?

A DOS program I think, a console is created when I double click on the .exe
which was built using the command 'bcc32 main'.

What do I need to do to get the -N option (check stack overflow) to work
btw?  It doesn't seem to help or do anything.

Gaz

Re:Maximum Size Of An Array?


Quote
"Gaz" <garyj...@hotmail.com> writes:
> > Using which compiler and which version?

> Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

Then this is the wrong newsgroup. You are using C++ Builder (even if it doesn't
always say so), and this newsgroup is about Borland C++. Please direct your
browser at http://www.borland.com/newsgroups and read the newsgroup
descriptions and guidelines to find the appropriate group for your question.

Quote
> A DOS program I think, a console is created when I double click on the .exe
> which was built using the command 'bcc32 main'.

C++ Builder does't produce DOS programs. You are creating a Win32 console
program.

Re:Maximum Size Of An Array?


Quote
Thomas Maeder <mae...@glue.ch> wrote:
>> A DOS program I think, a console is created when I double click on the .exe
>> which was built using the command 'bcc32 main'.

>C++ Builder does't produce DOS programs. You are creating a Win32 console
>program.

Which puts the maximum size of an array somewhere in the neighborhood
of 2 GB.

Now, what did your code look like again?
Oh yeah...
I think if you try it this way, it will work.

#include <iostream>
 int x[30000000L];      /* Move this here (120Mb) */
int main() {
 std::cout << "Here\n";
 std::cout << "Hello, World!\n";

Quote
}

Of course, you could always use new() or GlobalMalloc().

I'm pretty sure Wayne was correct about the array being too big for
your (default) stack. By using a DEF file, you can increase your
stack.

Re:Maximum Size Of An Array?


Quote
> int x[30000000L];

What does the 'L' bit do?

Thanks, Gaz

Re:Maximum Size Of An Array?


Quote
"Gaz" <garyj...@hotmail.com> writes:
> > int x[30000000L];

> What does the 'L' bit do?

It causes the type of the integer literal to be long int (or unsigned long int,
if its value is too large to be represented as a long int).

Other Threads