By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,428 Members | 1,851 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,428 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Nov 15 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a

zehanw...@gmail.com wrote:
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?

Thanks for your help.


Nov 15 '05 #2

P: n/a
ze*******@gmail.com wrote:
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 <stdlib.h>
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] = 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] = 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.
Nov 15 '05 #3

P: n/a
ze*******@gmail.com 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?

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

P: n/a
Keith Thompson wrote:

ze*******@gmail.com 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 :)

--
pete
Nov 15 '05 #5

P: n/a
pete wrote:
Keith Thompson wrote:
ze*******@gmail.com 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.

S.
Nov 15 '05 #6

P: n/a
On 2005-11-11, Skarmander <in*****@dontmailme.com> wrote:
pete wrote:
Keith Thompson wrote:
ze*******@gmail.com 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."

--
Neil Cerutti
Nov 15 '05 #7

P: n/a
Neil Cerutti <le*******@email.com> writes:
On 2005-11-11, Skarmander <in*****@dontmailme.com> wrote:
pete wrote:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8

P: n/a
Thanks for your help!

Nov 15 '05 #9

P: n/a
On 11 Nov 2005 21:41:50 +0100, in comp.lang.c , Neil Cerutti
<le*******@email.com> wrote:

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


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

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #10

P: n/a
Mark McIntyre wrote:
On 11 Nov 2005 21:41:50 +0100, in comp.lang.c , Neil Cerutti
<le*******@email.com> wrote:

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

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.
Nov 15 '05 #11

P: n/a
In article <u3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
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
Nov 15 '05 #12

P: n/a
In article <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
ze*******@gmail.com 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?


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).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #13

P: n/a
In article <dl***********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.uk> wrote:
In article <u3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
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.


[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.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 15 '05 #14

P: n/a
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

Nov 15 '05 #15

P: n/a
In article <dl**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
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
Nov 15 '05 #16

P: n/a
On 2005-11-13, Richard Tobin <ri*****@cogsci.ed.ac.uk> wrote:
In article <u3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
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.


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

--
Neil Cerutti
Nov 15 '05 #17

P: n/a

Dik T. Winter wrote:
1. int v[5] declares an array that can be indexed from 0 to 5, v[5]
does not exist.


I think you meant indexed from 0 to 4 :-)

Nov 15 '05 #18

P: n/a
Neil Cerutti wrote:
On 2005-11-13, Richard Tobin <ri*****@cogsci.ed.ac.uk> wrote:
In article <u3********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:

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.

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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.