LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 05-27-2012, 08:13 AM   #1
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Rep: Reputation: Disabled
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.
 
Old 05-27-2012, 08:32 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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 09:10 AM. Reason: Fixed bug when allocating rows.
 
Old 05-27-2012, 08:52 AM   #3
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
I take segmentation fault again!
 
Old 05-27-2012, 08:54 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by Thodoris21 View Post
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.
 
Old 05-27-2012, 09:00 AM   #5
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
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...
 
Old 05-27-2012, 09:02 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by Thodoris21 View Post
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).
 
Old 05-27-2012, 09:04 AM   #7
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
And on my system, stack size limit is set to 8192 Kbytes.I did it using malloc but i take the same message.
 
Old 05-27-2012, 09:13 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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;
}
 
Old 05-27-2012, 09:23 AM   #9
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Nothing again!What is going on?I run Ubuntu 12.04 on Vmware through windows 7.
 
Old 05-27-2012, 09:38 AM   #10
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Ok i fix it.It was my mistake!Thanks!
 
Old 05-27-2012, 09:38 AM   #11
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by Thodoris21 View Post
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 09:45 AM.
 
Old 05-27-2012, 09:48 AM   #12
Thodoris21
LQ Newbie
 
Registered: May 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thank you very much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing 2 large arrays ansrewdps Programming 5 08-05-2011 10:20 PM
LXer: This week at LWN: Large pages, large blocks, and large problems LXer Syndicated Linux News 0 09-27-2007 12:40 PM
Fortran Code with Large arrays statically allocated -> segmentation fault eiffel Linux - Kernel 2 07-19-2006 11:14 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 06:40 AM
C++ Segfault with Large Arrays guygriffiths Programming 3 05-15-2005 07:17 AM


All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration