Thread: understanding dynamic matrix declaration

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    understanding dynamic matrix declaration

    Dear all,
    It's been a long time that I haven't done C and I've forgot almost all about it. I'm trying to catch up with it!
    Well, first I need to understand one part i.e. dynamic matrix declaration; specifically, I am trying to understand these lines of code:


    Code:
    // 0. write Data matrix
    	int size_x = 210;
    	int size_y = 7;
    	double **Data, *pDataData;
    	Data = (double **)malloc(size_x*sizeof(double *)+size_x*size_y*sizeof(double));
    	for (i = 0, pDataData = (double *)(Data+size_x); i < size_x; i++, pDataData += size_y)
    	Data[i]=pDataData;
    
    	for(i=0;i<size_x;i++)
    		for(j=0;j<size_y;j++){
    			Data[i][j] = *(Data_in+size_y*i+j);
    			//printf("%f        ", Data[i][j]);
    		}	//printf("\n\n\n\n\n");
    Especially, I have problem understanding the bold lines. Could you try to explain it in a very clear way what exactly is going on here. I mean I know (by book) what malloc does and all that I just don't understand the details like what this initialization does:
    Code:
    pDataData = (double *)(Data+size_x)
    and how a pointer to pointer equals a pointer in this line:
    Code:
    Data[i]=pDataData
    Thank you very much in advance
    Elnaz
    Last edited by Elnaz; 01-14-2011 at 07:40 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're going to talk about bold lines, you should probably make some lines bold.

    Anyway, to take the second part first, Data[i] is a pointer, and pDataData is a pointer, consequently any worry about pointers to pointers is irrelevant because you don't have any.

    In the first part, Data+size_x is pointer arithmetic: you take the pointer Data, and you move it forward by size_x things-that-it-points-to. Data points to double*, so we move forward size_x * size-of-a-pointer-to-double-on-your-system bytes. That then gets cast to a double* before being assigned to pDataData.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Thank you, I got your points; the code is supposed to allocate a 210 by 7 matrix. I get that it first allocates a 210 by 7 memory block using malloc but why then it further adds a 210 length array to that block?
    It then goes on to write the addresses of each row into Data[i] (I assume) but again why going forward by 210 at first?
    I'm kind of not getting the structure here.

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    What do you in malloc is allocating data for 210 double * and 210*7 double. It looks like the first 210 elements are double *. You need pdatadata to point to the double, not double*, so you add 210 to Data to access the 211st elements on the heap.
    Last edited by nimitzhunter; 01-14-2011 at 09:34 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    In other words, what I still don't get is that in the 2nd block of code:
    Code:
    for(i=0;i<size_x;i++)
    		for(j=0;j<size_y;j++){
    			Data[i][j] = *(Data_in+size_y*i+j);
    			}
    we're feeding the values to the Data matrix; so, why do we feed Data[i] with the addresses of the rows of the matrix in the first step because eventually it will be filled with the values anyway. Why to bother fill it with some addresses that will be overwritten by values eventually??
    I'm sure I'm missing something here.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to provide Data[i] addresses, otherwise you won't be able to even contemplate Data[i][j]. If you don't know where Data[i] is, how can you do "j spaces after Data[i]" (which is what Data[i][j] means, after all)?

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    So, the first
    Code:
    size_x*sizeof(double *)
    memory block is allocated for this? for Data[i]s to include some addresses? We need more space in memory than the actual size of the matrix. We need the actual size of the matrix plus a vector to include the addresses of the rows separately. Is that it?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elnaz View Post
    So, the first
    Code:
    size_x*sizeof(double *)
    memory block is allocated for this? for Data[i]s to include some addresses? We need more space in memory than the actual size of the matrix. We need the actual size of the matrix plus a vector to include the addresses of the rows separately. Is that it?
    Got it in one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix multipilation with mpi
    By ahmed eltaher in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2010, 09:19 AM
  2. C program - inverse of a matrix
    By chaugh in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 11:00 PM
  3. problem with matrix size declaration
    By angelica in forum C++ Programming
    Replies: 13
    Last Post: 04-11-2008, 09:03 AM
  4. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  5. Replies: 1
    Last Post: 03-08-2006, 06:47 PM