memory allocation


sunil_josh1
11-19-2003, 07:02 AM
hi,
i am using lud for finding inverse of a matrix in c.
my programme is working , if the size of the matrix is upto 999x999. but when i am increasing the size of the matirix, programme is not working. how to use this code if i have big matrix like 20000x20000.
here i am writing code which i am using for allocating memory:
float f[n][n],c[n][n];
float **ff,**cc;
ff = (float**)malloc((unsigned)(n*sizeof(float*));
cc = (float**)malloc((unsigned)(n*sizeof(float*));
for(index=0;index<n;index++)
{
ff[index]=f[index];
cc[index]=c[index];

}
thanks
sunil:confused:

Kevin Dolan
12-09-2003, 03:53 PM
What error are you getting, and at what point in the program?

It is often the case that the maximum size of an array that can be statically allocated is much less than your physical memory limitations. I would guess that this is your problem. Try the following:

float *f,*c;
float **ff,**cc;
f = (float*)malloc((unsigned)(n*n*sizeof(float));
c = (float*)malloc((unsigned)(n*n*sizeof(float));
ff = (float**)malloc((unsigned)(n*sizeof(float*));
cc = (float**)malloc((unsigned)(n*sizeof(float*));
for(index=0;index<n;index++)
{
ff[index]=f+index*n;
cc[index]=c+index*n;
}

This should also be more efficient, there is no need to allocate f and c as two-dimensional arrays if you are just going to allocate a seperate array of pointers anyway. In your code, f and c are already arrays of pointers. All you did was make copies of them.

Kevin

sunil_josh1
12-09-2003, 06:34 PM
thank you very much kavin

sunil_josh1
01-16-2004, 02:54 PM
hi
can we use this code , if our matrix is in the form of double. If yes can u plz tell me how.
thanks
sunil_josh1

sunil_josh1
01-18-2004, 01:27 PM
hi,
here i am giving one small example of my input matrix for lud, to fine matrix inverse. This code is working well if i have input matrix as a flaot. but in case of double, it is giving wrong result.Below this mail i am giving my input matrix as well as output inverse matrix and at the end i am also giving ,small part of code , which i am using in main.
M=
0.000977 0.003662 0.009766 0.020752
0.003662 0.020752 0.063232 0.142822
0.009766 0.063232 0.200195 0.460205
0.020752 0.142822 0.460205 1.066650

f=
151795.921875 -291873.062500 211651.093750 -55188.652344
-291873.093750 583746.125000 -429104.812500 112652.851562
211650.328125 -429104.406250 317475.843750 -83636.109375
-55188.605469 112652.765625 -83636.078125 22075.365234

float flag;

int *indexx = (int*)malloc(((No+1)*(No+1))*sizeof(int));
float *coll = (float*)malloc(((No+1)*(No+1))*sizeof(float));

ludcmp(M,(No),indexx,&flag);

for(j=1;j<=(No);j++)
{
for(i=1;i<=No;i++)
coll[i]=0;
coll[j]=1;
lubksb(M,(No),indexx,coll);

for(i=1;i<=(No);i++)
f[i][j]=coll[i];
}

printf("\ncheck distances*****1 \n");
/************************************************** *******************************************/
for (j=1; j <= (No); j++)
{
for (i = 1; i <= (No); i++)
{

printf("%f \t",f[i][j]);
}
printf("\n");
}
with regards,
sunil_josh1

amitraj
02-27-2004, 04:44 AM
Hello sunil,
I think you should use "huge" pointer, which allows you to work on even segment boundries.
I have not tried "your" program, using a huge pointer; but fact is, when you want to use an array of such a big size, (more than one segment) we have to ask for a FAR or HUGE pointer to MALLOC().
For details you may refer to TINY, SMALL, COMPACT, MEDIUM, LARGE and HUGE models of memory in C

Regards