필터 지우기
필터 지우기

how to allocate memory for these arrays.. in mex

조회 수: 2 (최근 30일)
Abeera Tariq
Abeera Tariq 2015년 5월 28일
편집: Abeera Tariq 2015년 6월 2일
i am trying to compile a mex file. which has variable sized arrays and i found out these arrays should be dynamically allocated.. Here i tried to allocate the arrays but still i am getting error. Can someone check my mex file and arrays. Here Z is output which is array of variable size how to accommodate this?
float *B;
B= (float *)malloc(sizeof(float)*rmax*cmax*f2);
float B[rmax*cmax][f2];
free(B);

채택된 답변

James Tursa
James Tursa 2015년 5월 28일
편집: James Tursa 2015년 5월 28일
It is not entirely clear what you need help with. Are you trying to dynamically create 2D or 3D arrays of variable size? I.e., are you trying to create arrays on the fly with variable size, but still use the [ ][ ][ ] indexing syntax? If so, the answer is you can't do this directly in C (although you could easily do this in that "ancient" language called Fortran). My advice is to abandon the [ ][ ][ ] indexing syntax in your function. There is a way to do it, but you have to allocate pointer arrays off to the side to facilitate the syntax. Instead, just do the indexing manually. E.g., to create a "2D" matrix dynamically of size [A][B], one could do this:
int A = whatever;
int B = whatever;
// double x[A][B] substitute:
double *x;
x = (double *) mxMalloc( A * B * sizeof(*x) );
Then whenever you want to reference x[i][j] in your code, you would write this instead:
x[j+i*B]
Another little trick that some programmers use is to create a macro that facilitates the indexing. E.g.,
#define X(i,j) x[(j)+(i)*B]
Then whenever you would normally have written x[i][j], you can just write X(i,j) instead.
I will make another observation as well. In your text you state "... Z is output which is array of variable size ...". Are you aware that none of the X, I, and z variables in the function argument list are arrays? They are all pointers. The first level of [ ] is always interpreted as "pointer to" when used in a function argument list.
  댓글 수: 1
Abeera Tariq
Abeera Tariq 2015년 6월 1일
printmat.c(68) : error C2109: subscript requires array or pointer type
printmat.c(68) : error C2109: subscript requires array or pointer type
printmat.c(69) : error C2109: subscript requires array or pointer type
printmat.c(69) : error C2109: subscript requires array or pointer type
printmat.c(69) : error C2168: 'pow' : too few actual parameters for intrinsic function
I am facing these errors now.. with following piece of code..
for (i = 0; i<f2 ; i++) {
for (j = 0 ; j < (rmax*cmax); j++) {
\* line 68 *\ sum[j][i] = B[j][i] - v[i];
\* line 69 *\ sum1[j][i]= pow(sum[j][i],2); }}
I don't know how to access the array with sum[j][i],sum1[j][i] indexes..

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by