Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-27-2012, 07:13 AM
|
#1
|
|
LQ Newbie
Registered: May 2012
Posts: 27
Rep: 
|
Declare large arrays in C
Do you know how to declare large int arrays?
For example i want to declare a 2d array.I declare it as int A[4096][4096] but when the program starts it shows me segmentation fault.I think with malloc i can do it but i'm not sure.
|
|
|
|
05-27-2012, 07:32 AM
|
#2
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,388
|
When you declared the 2d array on the stack, you most likely exceeded the limits imposed by your system. You can check the stack size limit by issuing the command 'ulimit -s'. On my system, it is set to 8192 Kbytes. Your array far exceeds this size (btw, I believe your array has a size of 65536 Kbytes).
If you want to allocate from the heap this large array, then use something like:
Code:
#include <stdlib.h>
...
const int ROWS = 4096;
const int COLS = 4096;
int** array = malloc(ROWS * sizeof(int*)); // allocate the rows
for (int r = 0; r < ROWS; ++r)
{
array[r] = malloc(COLS * sizeof(int)); // allocate the columns
}
...
array[10][50] = 10; // insert data at row 10, column 50
...
for (int r = 0; r < ROWS; ++r)
{
free(array[r]); // this frees the columns
}
free(array); // this frees the rows
An alternative is to declare a 1d array, and then programmatically manage access to the 1d array as if it were a 2d array; for example:
Code:
Code:
#include <stdlib.h>
...
const int ROWS = 4096
const int COLS = 4096;
int* array = malloc(ROWS * COLS * sizeof(int));
...
array[10 * ROWS + 50] = 10; // insert data at row 10, column 50
...
free(array); // frees allocated memory
Last edited by dwhitney67; 05-27-2012 at 08:10 AM.
Reason: Fixed bug when allocating rows.
|
|
|
|
05-27-2012, 07:52 AM
|
#3
|
|
LQ Newbie
Registered: May 2012
Posts: 27
Original Poster
Rep: 
|
I take segmentation fault again!
|
|
|
|
05-27-2012, 07:54 AM
|
#4
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,388
|
Quote:
Originally Posted by Thodoris21
I take segmentation fault again!
|
You will need to post your code (within CODE tags!) to get additional help. Your comment above is of no help.
|
|
|
|
05-27-2012, 08:00 AM
|
#5
|
|
LQ Newbie
Registered: May 2012
Posts: 27
Original Poster
Rep: 
|
Ok this is my code
Code:
#define dim1 4096
#define dim2 4096
int main (int argc, char *argv[]) {
int prevkMap[dim1][dim2],num,i,j;
for (i=0; i<dim1; i++) {
for (j=0; j<dim2; j++) {
num=rand() % 6;
prevkMap[i][j]=num;
}
}
return 0;
}
When dim1 and dim2 is 512 it runs without problem.With values above 512 there is problem...
|
|
|
|
05-27-2012, 08:02 AM
|
#6
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,388
|
Quote:
Originally Posted by Thodoris21
When dim1 and dim2 is 512 it runs without problem.With values above 512 there is problem...
|
Again, from my previous post...
Quote:
|
When you declared the 2d array on the stack, you most likely exceeded the limits imposed by your system. You can check the stack size limit by issuing the command 'ulimit -s'. On my system, it is set to 8192 Kbytes. Your array far exceeds this size (btw, I believe your array has a size of 65536 Kbytes).
|
|
|
|
|
05-27-2012, 08:04 AM
|
#7
|
|
LQ Newbie
Registered: May 2012
Posts: 27
Original Poster
Rep: 
|
And on my system, stack size limit is set to 8192 Kbytes.I did it using malloc but i take the same message.
|
|
|
|
05-27-2012, 08:13 AM
|
#8
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,388
|
This works fine for me:
Code:
#include <stdlib.h>
#include <time.h>
int main()
{
const int ROWS = 4096;
const int COLS = 4096;
int** prevkMap = malloc(ROWS * sizeof(int*));
for (int r = 0; r < ROWS; ++r)
{
prevkMap[r] = malloc(COLS * sizeof(int));
}
srand(time(NULL));
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
prevkMap[r][c] = rand() % 6;
}
}
// TODO: Free allocated memory.
return 0;
}
|
|
|
|
05-27-2012, 08:23 AM
|
#9
|
|
LQ Newbie
Registered: May 2012
Posts: 27
Original Poster
Rep: 
|
Nothing again!What is going on?I run Ubuntu 12.04 on Vmware through windows 7.
|
|
|
|
05-27-2012, 08:38 AM
|
#10
|
|
LQ Newbie
Registered: May 2012
Posts: 27
Original Poster
Rep: 
|
Ok i fix it.It was my mistake!Thanks!
|
|
|
|
05-27-2012, 08:38 AM
|
#11
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,679
|
Quote:
Originally Posted by Thodoris21
Do you know how to declare large int arrays?
For example i want to declare a 2d array.I declare it as int A[4096][4096] but when the program starts it shows me segmentation fault.I think with malloc i can do it but i'm not sure.
|
You could do it with malloc, but if you want to use the stack you can do so.
You just need a little extra code to allow use of an excessive size stack. The following program adjusts its own stack limit to 1MB greater than what is needed for a 4Kx4K int array, then it calls a function that uses that 4Kx4K array.
Code:
#include <sys/resource.h>
void stack_consumer()
{
int A[4096][4096];
A[0][0] = 1;
A[4095][4095] = 1;
printf ("A was allocated at address %x\n", A );
}
int main (int argc, char **argv)
{
const rlim_t StackSize = sizeof(int) * 4096 * 4096 + 1048576;
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
printf("Changing stack limit from %d to %d\n", rl.rlim_cur, StackSize );
rl.rlim_cur = StackSize;
result = setrlimit(RLIMIT_STACK, &rl);
}
stack_consumer();
return 0;
}
Last edited by johnsfine; 05-27-2012 at 08:45 AM.
|
|
|
|
05-27-2012, 08:48 AM
|
#12
|
|
LQ Newbie
Registered: May 2012
Posts: 27
Original Poster
Rep: 
|
Thank you very much!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:13 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|