Thread: dynamically allocating a 2d array

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    39

    dynamically allocating a 2d array

    Hi,

    what is the syntax for dynamically allocating a 2d array. My 2d array has large dimensions and so have to be dynamically allocated.

    Code:
    // I want  double array[5000][5000]
    
    // so I did this, which does not work
    
    double * array[5000];
    array = malloc(sizeof(double) * 5000 * 5000)
    How do I do this?
    Thanks,

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    yes that work. thanks. I have a different problem now ( I have been working on the problem continuously so sorry, if it seems like I am asking too many questions).

    Previously I did as is shown in the link.
    Code:
    double **array1 = malloc(5000 * sizeof(double ));
    	for(i = 0; i < 5000; i++)
    		array1[i] = malloc(5000 * sizeof(double));
    and I get array1[5000][5000] as desired. Now I have to use array1 in a struct as follows.

    Code:
    struct solution { int ell;
                           double array[5000][5000];
                          };
    
    // now define 1200 of these structs
    
    struct solution *my_Sol;
    // doing the following return a NULL pointer. I am guessing there is no single 
    // block of memory that big. I'd have to allocate smaller blocks like above
    // but the problem is the technique for 2d arrays deals with a single data type
    
    my_Sol = malloc(sizeof(struct solution) * 1200)) //does not work
    
    //nor does the following
    int ell;
    for (ell = 0; ell < 1200; ell++)
    {
       my_Sol[ell] =malloc(sizeof(struct solution));
    }
    How should I approach the problem?

    System Monitor shows that I am using 46% of 1.9GiB right now.
    Last edited by ali.franco95; 10-18-2011 at 09:17 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    5000*5000*8*1200 = 240000000000
    240000000000 / 1073741824 = 223.517

    That's the number of GB you are asking for. I don't know what the problem you are working on is, so I don't have any suggestions about how to reduce that. If you can't, you could create one struct at a time then write it to disk, and access them that way (ie, use a database instead of holding everything in memory). That might take a bit of planning, based on what it is you want to do with the values.
    Last edited by MK27; 10-18-2011 at 09:32 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 01-09-2009, 01:09 PM
  2. Dynamically allocating array size
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 06:24 AM
  3. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 04:07 PM
  4. Dynamically Allocating a 2D Array.
    By LightsOut06 in forum C Programming
    Replies: 17
    Last Post: 10-09-2005, 12:55 AM
  5. dynamically allocating a 2dim array
    By agerealm in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:40 PM