Find the answer to your Linux question:
Results 1 to 8 of 8
I need four dimensional array like double a[256][256][256][256]; but the size is too big to use an array. If I use only pointer like double ****a; to allocate this memory, ...
  1. #1
    Just Joined!
    Join Date
    Feb 2007
    Posts
    31

    allocation of four dimensional array memory

    I need four dimensional array like
    double a[256][256][256][256];


    but the size is too big to use an array.

    If I use only pointer like
    double ****a;

    to allocate this memory, I have to run 3 loops to allocate memory ..which takes lots of time.
    a = (double **** ) malloc (sizeof(double ***)*256);
    for(i=0;i<256;i++)
    {
    a[i] = (double ***) malloc ( sizeof(double **)*256);
    for(j=0;j<256;j++)
    {
    a[i][j] = (double **) malloc ( sizeof(double *)*256);
    for(k=0;k<256;k++)
    {
    a[i][j][k] = (double *) malloc ( sizeof(double )*256);
    }
    }
    }


    is there any other work around ....?

    I was thinking to use *a[256][256][256]
    Can I use this ...and if yes how I can allocate the memory for this...?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you say something like
    Code:
    double a[256][256][256][256];
    then you're asking for 32 gigabytes of main memory. The compiler will choke on this, for very good reasons.

    Your only hope is to allocate the memory for this in your heap by using malloc(). But even that won't work if you're running 32 bit Linux, because you'd need at least a 36 bit address space for this.

    Consider placing this data on disk.

    Are you running 64-bit Linux? If so it's theoretically possible to put this data in your heap by using malloc(). If that's the case, let's discuss this further.

    Hope this helps.

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    3
    Also, a much better way to allocate this (although, like the previous poster mentioned, this is A LOT of data), would be something like:

    double *array = (double*)malloc(256*256*256*256*sizeof(double));

    You'll have to manage your own indexing, but it's not too hard

  4. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    There is no reason to have an array that big.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quote Originally Posted by likwid View Post
    There is no reason to have an array that big.
    And you know this how, exactly?

  6. #6
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Well, let me rephrase, unless you have a gigantic amount of static data you need super fast access to, like the kind of stuff you would partition out in an HPC cluster, I see no reason to create an array that big.

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    Hey, that works for me!
                          ^
                          |
    (See?  An ASCII smiley, the way God intended!)

  8. #8
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Heh, I agree with you wje, use the disk. If the array isn't ever really going to be full maybe a sparse array. A little more detail as to the application and surely some of the crowd here can help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...