473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to create huge two-dementional array

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!
Nov 15 '05 #1
5 1694
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[i] = malloc(NCOLS * sizeof(int));
if (arr[i] == NULL) {
exit(EXIT_FAILURE);
}
}

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

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

return 0;
}

-David

Nov 15 '05 #2

"David Resnick" <ln********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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[i] = malloc(NCOLS * sizeof(int));
if (arr[i] == 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.
Nov 15 '05 #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

Nov 15 '05 #4
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
Nov 15 '05 #5
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> 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) 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 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: john67 | last post by:
The company I work for is about to embark on developing a commercial application that will cost us tens-of-millions to develop. When all is said and done it will have thousands of business...
15
by: Beda Christoph Hammerschmidt | last post by:
I wat to perform some performance measurements on an XML database. FOr this reason i need some huge XML sample data. The data should be not too structured and a lot of reasonable queries should...
1
by: Jim, N2VX | last post by:
I'd like to create/display an Excel spreadsheet from javascript. We have an HTML page with results of a search and it can be reasonably large. The first attempt was to format the data into an...
3
by: Esger Abbink | last post by:
Hello, it is very possible that this is a well described problem, but I have not been able to find the solution. On two production server (7.2rc2) of ours the data directory is growing to...
2
by: WideBoy | last post by:
Hi I have access to a large xml schema namespace which has complicated references, structures, etc for all sorts of things. Amongst which are personDetails, and their UK postal address. What I...
3
by: Gummy | last post by:
Hello, I have an ASPX page on which I place a UserControl 15 times (they only need to be static controls on the page). This UserControl is a set of two listboxes with radiobuttons above the...
13
by: fAnSKyer/C# newbie | last post by:
My system has 4GB memory and My program in C# is really memory consuming. and I noticed that when the memory I used is more than 2GB I will get an exception. How to solve this? thanks a lot
26
by: sam_cit | last post by:
Hi Everyone, I'm in a need to allocate huge memory block of size 50kb. How can i use malloc() for this purpose, asuming sizeof(int) is two bytes? Thanks in advance!!!
2
by: lazy | last post by:
Hi, I have a dictionary something like this, key1=>{key11=> , key12=> , .... } For lack of wording, I will call outer dictionary as dict1 and its value(inner dictionary) dict2 which is a...
25
by: Licheng Fang | last post by:
I mean, all the class instances that equal to each other should be reduced into only one instance, which means for instances of this class there's no difference between a is b and a==b. Thank...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.