Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

any body knows how to convert 2d dynamic array to static so that i can use it in lapacke. dgels function which only take static matrix in c? when i use malloc it does not give correct answer. how can i use malloc so that it works with it.thankyou

#include <stdio.h>
#include <lapacke.h>
#include <conio.h>

int main (int argc, const char * argv[])  
{
    /*double a[5][3] = {1,1,1,2,3,4,3,5,2,4,2,5,5,4,3};*/

    double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
    lapack_int info,m,n,lda,ldb,nrhs;
    int i,j;
    double **a;

    a=(double**)malloc(5* sizeof(double*));
    for (i=0;i<5;i++)
    {
        a[i]=(double*)malloc(3* sizeof(double));
    }
    a[0][0]=1; 
    a[0][1]=1;
    a[0][2]=1;
    a[1][0]=2;
    a[1][1]=3;
    a[1][2]=4;
    a[2][0]=3;
    a[2][1]=5;
    a[2][2]=2;
    a[3][0]=4;
    a[3][1]=2;
    a[3][2]=5;
    a[4][0]=5; 
    a[4][1]=4;
    a[4][2]=3;

    m = 5;
    n = 3;
    nrhs = 2;
    lda = 3;
    ldb = 2;

    info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*b,ldb);

    for(i=0;i<n;i++)
    {
        for(j=0;j<nrhs;j++)
        {
            printf("%lf ",b[i][j]);
        }
        printf("\n");
    }
    getch();
    return(info);
}
share|improve this question
add comment

2 Answers

I do not know lapacke.dgels but try to change:

double **a;
a=(double**)malloc(5* sizeof(double*));
for (i=0;i<5;i++)
{
    a[i]=(double*)malloc(3* sizeof(double));
}

to:

double (*a)[3];
a = malloc(5 * 3 * sizeof(double));
share|improve this answer
1  
Exactly, dgels needs a contiguous memory area. Intel has some examples dgels. –  Timothy Brown 4 hours ago
    
That's clever, I didn't think of how to get the compiler to do the index calculations for you. –  pat 4 hours ago
    
@marian: your program works: but in my real program which is big: this initialization of double (*a)[3]; is in the middle of the program: so i have to use specific [designs]. instead of it[3]. so than how should i write malloc? –  user3546028 3 hours ago
add comment

a is not a 2d array, it is an array of pointers to separate 1d arrays. Passing *a to LAPACKE_dgels only gives it a pointer to the first row. It will have no way to know where all of the other rows were allocated since they were allocated independently. It wants the entire array to be in a single contiguous block of memory. a must be of type double*, not double**, and you don't dereference it when passing it. You must flatten the 2d indexes into 1d indexes yourself, using either row or column major form (which you tell the function).

EDIT

The following code allocates a flat 1d array with room for m*n doubles. It then fills the array by converting the 2d indices to 1d row-major indices using the formula row * n + col. If we wanted column-major indices, we would use col * m + row.

#include <stdio.h>
#include <lapacke.h>
#include <conio.h>

int main (int argc, const char * argv[])  
{
    double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
    lapack_int info,m,n,lda,ldb,nrhs;
    int i,j;
    double *a;

    m = 5;
    n = 3;
    nrhs = 2;
    lda = 3;
    ldb = 2;

    a = malloc(m * n * sizeof(double));
    a[0 * n + 0] = 1;
    a[0 * n + 1] = 1;
    a[0 * n + 2] = 1;
    a[1 * n + 0] = 2;
    a[1 * n + 1] = 3;
    a[1 * n + 2] = 4;
    a[2 * n + 0] = 3;
    a[2 * n + 1] = 5;
    a[2 * n + 2] = 2;
    a[3 * n + 0] = 4;
    a[3 * n + 1] = 2;
    a[3 * n + 2] = 5;
    a[4 * n + 0] = 5; 
    a[4 * n + 1] = 4;
    a[4 * n + 2] = 3;

    info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,a,lda,*b,ldb);

    for(i=0;i<n;i++)
    {
        for(j=0;j<nrhs;j++)
        {
            printf("%lf ",b[i][j]);
        }
        printf("\n");
    }
    getch();
    return(info);
}
share|improve this answer
    
how can i a flatten them to 1d array using row or column major for? i am not aware of this funtions? can you give the example by taking malloc from above. because i am working in 2d array malloc from the beginning and than i have to create 1d malloc. i dont have any idea: thanx –  user3546028 3 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.