how to create huge two-dementional array

W

Winston

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!
 
D

David Resnick

Winston said:
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
 
R

Robert Gamble

Winston said:
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
 
W

Winston

David Resnick said:
Winston said:
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.
 
E

E. Robert Tisdale

Winston said:
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
 
K

Keith Thompson

E. Robert Tisdale said:
Winston said:
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.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top