Help:How can I create a Huge array with 10000000 elements?

Z

zehanwang

Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

Thanks for your help.
 
M

mensanator

Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

Because arrays index from 0?
 
S

Skarmander

Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"
If you want to include system headers, use angular brackets:

#include <stdio.h>
#include said:
int main() {
int v[10000000];
long i;

If you're indexing an array, size_t is a more suitable type than long,
although it's not wrong. (Hey, that rhymes.)
for (i=1; i<=10000000; i++) v [ i ]=i;

Your program accesses v[10000000], which is not part of the array. It
only goes up to v[9999999], since arrays start from 0 in C. Write the
loop like this:

for (i = 0; i < 10000000; i++) v = i;

Or better yet (and look up the details of this if you don't understand it):

for (i = 0; i < sizeof v / sizeof v[0]; i++) v = i;

Finally, note that the values you're trying to assign to the array
elements exceed the maximum value guaranteed to fit in an int (32767).
They will fit on a 32-bit platform, but it is still good to keep in
mind. If you want to make sure these values can be stored, make the type
of the array elements `long', not `int'; then it will work on any platform.
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

Aside from the problems above, you're trying to use an array of 40
million bytes (I'm guessing you're compiling for a Win32 platform, which
has ints that are 4 bytes large). This exceeds the default stack of 1 MB
Visual C++ sets for applications.

If you *really* want your program to allocate 40 million bytes on
startup, tell the compiler that this should be allowed. Search for
"stack size" in the documentation. Failing that, make your array
smaller, or allocate it dynamically.

S.
 
K

Keith Thompson

Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

What error occurred?

Some systems impose limits on how big a variable can be. You might be
able to allocate more space with malloc() than with a local variable
declaration, but there are no guarantees.
 
P

pete

Keith said:
Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

What error occurred?

There was no visible output :)
 
S

Skarmander

pete said:
Keith said:
Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

What error occurred?


There was no visible output :)
Oh, I think he actually got one of those nice dialogue boxes that says
"Abort, retry, fail, ignore?"

Eh, slightly wrong OS. Never mind.

S.
 
N

Neil Cerutti

pete said:
Keith said:
(e-mail address removed) writes:

Compiler: Visual C++ 6.0
My code:
//==============================
# include "stdio.h"
# include "stdlib.h"

int main() {
int v[10000000];
long i;
for (i=1; i<=10000000; i++) v [ i ]=i;
return 0;
}
//==============================

No any errors or warnings while compiling or linking. But when I run
the executable file, an error ocured. WHY?

What error occurred?


There was no visible output :)
Oh, I think he actually got one of those nice dialogue boxes
that says "Abort, retry, fail, ignore?"

Eh, slightly wrong OS. Never mind.

I was hoping for: "Keyboard not found. Press any key to reboot."
 
K

Keith Thompson

Neil Cerutti said:
pete said:
Keith Thompson wrote: [snip]
What error occurred?


There was no visible output :)
Oh, I think he actually got one of those nice dialogue boxes
that says "Abort, retry, fail, ignore?"

Eh, slightly wrong OS. Never mind.

I was hoping for: "Keyboard not found. Press any key to reboot."

Press any key to continue. Press any other key to abort.
 
J

Joe Estock

Mark said:
The actual message with my mobo is
"Keyboard not found. Press any key to continue"
Heh, mine is F1 (on my laptop), but on my server it says "No keyboard
present, ignoring.". Iirc it's a setting in the BIOS.
 
R

Richard Tobin

Mark McIntyre said:
The actual message with my mobo is
"Keyboard not found. Press any key to continue"

Much as I hate to spoil an excellent story, this message is not as
contradictory as it appears. You can find a keyboard, plug it in, and
press a key to continue.

-- Richard
 
D

Dik T. Winter

> > Compiler: Visual C++ 6.0
> > My code:
> > //==============================
> > # include "stdio.h"
> > # include "stdlib.h"
> >
> > int main() {
> > int v[10000000];
> > long i;
> > for (i=1; i<=10000000; i++) v [ i ]=i;
> > return 0;
> > }
> > //==============================
> >
> > No any errors or warnings while compiling or linking. But when I run
> > the executable file, an error ocured. WHY?
>
> What error occurred?

I would expect something like a segmentation fault. When i == 10000000,
there is no associated array element. To the OP:
1. int v[5] declares an array that can be indexed from 0 to 5, v[5]
does not exist.
2. some systems restrict the amount of storage you may request in
local declarations (as here).
 
W

Walter Roberson

Much as I hate to spoil an excellent story, this message is not as
contradictory as it appears. You can find a keyboard, plug it in, and
press a key to continue.

[OT]
No, usually you cannot do that. If you have a PS/2 keyboard, then
usually plugging it in while the system is running will -not- result
in the keyboard being recognized. If you have a USB keyboard, then
when you are at that place in the PROM, plugging in the USB is unlikely
to get the drivers selected and activated.

The one case I can think of in which plugging in the keyboard at that
point would work, was with the serial keyboards that are rarely used
anymore.
 
A

Annajiat

Possible Problems:
Prob: 1. You are running our of stack space. (99% possibility)
Sol: 1. Make the array global.
Prob: 2. You don't have that much of free memory.
Sol: 2. Ensure that you have about 40MB free memory.

Yes, indexing is a problem,
write either for (i=0; i<10000000; i++) or for (i=1; i<10000000;
i++) whichever suits your intentions.


Corrected:

# include "stdio.h"
# include "stdlib.h"

int v[10000000];
int main() {
long i;
for (i=0; i<10000000; i++) v [ i ]=i;
return 0;
}




--
Thanking you
আনà§à¦¨à¦¾à¦¿à¦œà§Ÿà¦¾à¦¤ আলীম রােসল
Annajiat Alim Rasel
Secretary
BUCC
BRAC University Computer Club
BUCC: http://groups-beta.google.com/group/bucc
BUCC Programming Contest Wing:
http://groups.yahoo.com/group/buacm
http://groups-beta.google.com/group/buacm
 
R

Richard Tobin

Walter Roberson said:
No, usually you cannot do that. If you have a PS/2 keyboard, then
usually plugging it in while the system is running will -not- result
in the keyboard being recognized.

You may well be right, it's a long time since I tried it and it was
probably a serial keyboard.

-- Richard
 
N

Neil Cerutti

Much as I hate to spoil an excellent story, this message is not
as contradictory as it appears. You can find a keyboard, plug
it in, and press a key to continue.

It depends on the port. Those darn PS2 ports can actually fry if
you hot-plug 'em. They aren't designed for that.
 
J

Joe Wright

Neil said:
It depends on the port. Those darn PS2 ports can actually fry if
you hot-plug 'em. They aren't designed for that.
And once you fry the keyboard conroller you're in for a new motherboard.
The replacement part costs as much as the mainboard and the labor to
replace costs more than either.

Don't hot-plug anything! Or, keep lots of money around.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top