473,789 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1717
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_FAILU RE);
}

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

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

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

return 0;
}

-David

Nov 15 '05 #2

"David Resnick" <ln********@gma il.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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_FAILU RE);
}

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

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_Keit h) 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
3561
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 objects/classes, some of which will have hundreds-of-thousands of instances stored in a DB. Our clients will probably have somewhere between 50-200 users working on the app during the day, possibly in mutiple offices, and then a large number of batch...
15
1969
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 make sense. Any idea, where i can get this data ??
1
47749
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 HTML table and send it to an ASP page. The ASP page has: Response.AddHeader ("Content-Disposition", "inline"); Response.ContentType = "application/vnd.ms-excel"); Response.Write(formatted_html_data);
3
4303
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 very large sizes while the data that is actually in the db's isnt 1. that large and 2. growing. The databases see a fairly limited/constant use at the moment. The data
2
1354
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 would like to do is see what sort of instance document I need to create to test it's validity against this complex schema. The xml schema does not have a root node/element, perhaps this is because it is purely a namespace? - I don't know.
3
3490
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 listbox (to select between viewing a code or description in the listbox). There are also left and right arrows that move the selected items between the listboxes. In my Page_Load I assign each UserControl the appropriate DataTable and this...
13
5460
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
1719
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
2539
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 dictionary of small fixed size lists(2 items) The key of the dictionary is a string and value is another dictionary
25
1575
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 you.
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.