how to create huge two-dementional array

Discussion in 'C Programming' started by Winston, Jul 21, 2005.

  1. Winston

    Winston Guest

    Hello, There:

    I would like to create a huge two-dimentational array in C, like 5000X5000.
    However, I always got sementation faults when accessing more than 2000X2000.
    Is there any way to overcome this issue?

    Thanks in advance!
    Winston, Jul 21, 2005
    #1
    1. Advertising

  2. Winston wrote:
    > Hello, There:
    >
    > I would like to create a huge two-dimentational array in C, like 5000X5000.
    > However, I always got sementation faults when accessing more than 2000X2000.
    > Is there any way to overcome this issue?
    >
    > Thanks in advance!


    Show some code. If your code is valid, you may be hitting
    some limitations of your platform, which are best discussed
    in a newsgroup dedicated to that platform.

    This works for me:
    #include <stdio.h>
    #include <stdlib.h>

    #define NROWS 5000
    #define NCOLS 5000

    int main(void)
    {
    int i;
    int **arr = malloc(NROWS * sizeof(int *));

    if (arr == NULL) {
    exit(EXIT_FAILURE);
    }

    for(i = 0; i < NROWS; i++) {
    arr = malloc(NCOLS * sizeof(int));
    if (arr == NULL) {
    exit(EXIT_FAILURE);
    }
    }

    arr[NROWS-1][NCOLS-1] = 27;

    printf("%d\n", arr[NROWS-1][NCOLS-1]);

    return 0;
    }

    -David
    David Resnick, Jul 21, 2005
    #2
    1. Advertising

  3. Winston wrote:
    > Hello, There:
    >
    > I would like to create a huge two-dimentational array in C, like 5000X5000.
    > However, I always got sementation faults when accessing more than 2000X2000.
    > Is there any way to overcome this issue?


    Standard C only guarantees the ability to create an object of up to
    65535 bytes (in a hosted enviornment which I assume you are using).
    You didn't say what you were trying to create an array of, but even a
    2000x2000 array of char is at least 4,000,000 bytes.

    <OT>
    Many systems use a stack of limited size to place automatic variables.
    Your compiler or operating system may provide the ability to increase
    this size (see the appropriate documentation for details). On such
    systems, dynamically allocated memory is often placed in a heap whose
    size is dynamic and can grow much larger than the stack, you might want
    to try using malloc to allocate space for your array instead.
    </OT>

    Robert Gamble
    Robert Gamble, Jul 21, 2005
    #3
  4. Winston

    Winston Guest

    "David Resnick" <> wrote in message
    news:...
    > Winston wrote:
    > > Hello, There:
    > >
    > > I would like to create a huge two-dimentational array in C, like

    5000X5000.
    > > However, I always got sementation faults when accessing more than

    2000X2000.
    > > Is there any way to overcome this issue?
    > >
    > > Thanks in advance!

    >
    > Show some code. If your code is valid, you may be hitting
    > some limitations of your platform, which are best discussed
    > in a newsgroup dedicated to that platform.
    >
    > This works for me:
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #define NROWS 5000
    > #define NCOLS 5000
    >
    > int main(void)
    > {
    > int i;
    > int **arr = malloc(NROWS * sizeof(int *));
    >
    > if (arr == NULL) {
    > exit(EXIT_FAILURE);
    > }
    >
    > for(i = 0; i < NROWS; i++) {
    > arr = malloc(NCOLS * sizeof(int));
    > if (arr == NULL) {
    > exit(EXIT_FAILURE);
    > }
    > }
    >
    > arr[NROWS-1][NCOLS-1] = 27;
    >
    > printf("%d\n", arr[NROWS-1][NCOLS-1]);
    >
    > return 0;
    > }
    >
    > -David
    >

    Thanks! I tried another machine, and then it is working with a reasonable
    size limit.
    Winston, Jul 21, 2005
    #4
  5. Winston wrote:

    > I would like to create a huge two-dimentational array in C, like 5000X5000.
    > However, I always got sementation faults when accessing more than 2000X2000.
    > Is there any way to overcome this issue?


    > cat main.c

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char* argv[]) {
    const
    size_t NROWS = 5000;
    const
    size_t NCOLS = 5000;
    int arr[NROWS][NCOLS];

    arr[NROWS-1][NCOLS-1] = 42;

    fprintf(stdout, "arr[NROWS-1][NCOLS-1] = %d\n",
    arr[NROWS-1][NCOLS-1]);

    return 0;
    }

    > gcc -Wall -std=c99 -pedantic -o main main.c
    > ./main

    Segmentation fault (core dumped)
    > limit stacksize

    stacksize 10240 kbytes
    > limit stacksize unlimited
    > ./main

    arr[NROWS-1][NCOLS-1] = 42
    E. Robert Tisdale, Jul 21, 2005
    #5
  6. "E. Robert Tisdale" <> writes:
    > Winston wrote:
    >
    >> I would like to create a huge two-dimentational array in C, like 5000X5000.
    >> However, I always got sementation faults when accessing more than 2000X2000.
    >> Is there any way to overcome this issue?

    >
    > > cat main.c

    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char* argv[]) {
    > const
    > size_t NROWS = 5000;
    > const
    > size_t NCOLS = 5000;
    > int arr[NROWS][NCOLS];
    >
    > arr[NROWS-1][NCOLS-1] = 42;
    >
    > fprintf(stdout, "arr[NROWS-1][NCOLS-1] = %d\n",
    > arr[NROWS-1][NCOLS-1]);
    >
    > return 0;
    > }
    >
    > > gcc -Wall -std=c99 -pedantic -o main main.c
    > > ./main

    > Segmentation fault (core dumped)
    > > limit stacksize

    > stacksize 10240 kbytes
    > > limit stacksize unlimited
    > > ./main

    > arr[NROWS-1][NCOLS-1] = 42


    ERT appears to have lost the ability to communicate in English. What
    he seems to have meant (or what he should have meant) is that the
    limits on object size are system-specific, and that some systems may
    provide ways to increase those limits.

    The details, of course, are system-specific and off-topic here. Not
    all systems even have a "stack" in the sense implied here. The
    specific method he shows here may or may not be of any use to the
    original poster.

    --
    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, Jul 21, 2005
    #6
    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. Eric Wichterich
    Replies:
    1
    Views:
    431
    Peter Otten
    Nov 1, 2003
  2. GenxLogic
    Replies:
    3
    Views:
    1,245
    andrewmcdonagh
    Dec 6, 2006
  3. Replies:
    18
    Views:
    711
    Joe Wright
    Nov 15, 2005
  4. Replies:
    3
    Views:
    481
  5. Replies:
    1
    Views:
    256
    Joerg Meier
    Aug 14, 2013
Loading...

Share This Page