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

Discussion in 'C Programming' started by zehanwang@gmail.com, Nov 11, 2005.

  1. Guest

    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 11, 2005
    #1
    1. Advertising

  2. Guest

    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 11, 2005
    #2
    1. Advertising

  3. Skarmander Guest

    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;

    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.
     
    Skarmander, Nov 11, 2005
    #3
  4. 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) <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.
     
    Keith Thompson, Nov 11, 2005
    #4
  5. pete Guest

    Keith Thompson wrote:
    >
    > 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
     
    pete, Nov 11, 2005
    #5
  6. Skarmander Guest

    pete wrote:
    > Keith Thompson wrote:
    >
    >> 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.
     
    Skarmander, Nov 11, 2005
    #6
  7. Neil Cerutti Guest

    On 2005-11-11, Skarmander <> wrote:
    > pete wrote:
    >> Keith Thompson wrote:
    >>
    >>> 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
     
    Neil Cerutti, Nov 11, 2005
    #7
  8. Neil Cerutti <> writes:
    > On 2005-11-11, Skarmander <> 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) <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.
     
    Keith Thompson, Nov 11, 2005
    #8
  9. Guest

    Thanks for your help!
     
    , Nov 12, 2005
    #9
  10. On 11 Nov 2005 21:41:50 +0100, in comp.lang.c , Neil Cerutti
    <> 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 =----
     
    Mark McIntyre, Nov 12, 2005
    #10
  11. Joe Estock Guest

    Mark McIntyre wrote:
    > On 11 Nov 2005 21:41:50 +0100, in comp.lang.c , Neil Cerutti
    > <> 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.
     
    Joe Estock, Nov 13, 2005
    #11
  12. In article <>,
    Mark McIntyre <> 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
     
    Richard Tobin, Nov 13, 2005
    #12
  13. In article <> Keith Thompson <> writes:
    > 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/
     
    Dik T. Winter, Nov 13, 2005
    #13
  14. In article <dl62tm$1505$>,
    Richard Tobin <> wrote:
    >In article <>,
    >Mark McIntyre <> 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
     
    Walter Roberson, Nov 13, 2005
    #14
  15. Annajiat Guest

    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
     
    Annajiat, Nov 13, 2005
    #15
  16. In article <dl64eh$8g2$>,
    Walter Roberson <-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
     
    Richard Tobin, Nov 13, 2005
    #16
  17. Neil Cerutti Guest

    On 2005-11-13, Richard Tobin <> wrote:
    > In article <>,
    > Mark McIntyre <> 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
     
    Neil Cerutti, Nov 15, 2005
    #17
  18. 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 :)
     
    Michel Rouzic, Nov 15, 2005
    #18
  19. Joe Wright Guest

    Neil Cerutti wrote:
    > On 2005-11-13, Richard Tobin <> wrote:
    >
    >>In article <>,
    >>Mark McIntyre <> 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 ---
     
    Joe Wright, Nov 15, 2005
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P
    Replies:
    1
    Views:
    1,170
    Joe Kesselman
    Jul 7, 2006
  2. Eric Wichterich
    Replies:
    1
    Views:
    439
    Peter Otten
    Nov 1, 2003
  3. Winston

    how to create huge two-dementional array

    Winston, Jul 21, 2005, in forum: C Programming
    Replies:
    5
    Views:
    352
    Keith Thompson
    Jul 21, 2005
  4. Replies:
    3
    Views:
    510
  5. Replies:
    3
    Views:
    96
    Dr John Stockton
    Aug 3, 2006
Loading...

Share This Page