SDK compiler vs gcc

2 views (last 30 days)
lena
lena on 10 Sep 2013
I have an example where I want to create a vector of doubles of a size given by a variable i pass to a function within a mex function (double V[] and then fill it out using a loop). The code is compiled and works as expected with gcc compiler on a linux machine. However, when I try to compile the code on a 32 bits windows machine using a SDK compiler the line double V[] causes a few errors.
This is the code: **************************************************************** // #include matrix.h #include mex.h
void comp_routine(double c[],const int s){ int i; double V[s-1]; // this line doesn't cause trouble in gcc compiler // double V[10]; for (i=0;i<s;i++){ V[i]=i; }
for (i=0;i<s;i++){
c[i]=V[i]=i;
}
return;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
//declare variables
mxArray *a_in_m, *c_out_m;
double *a,*c;
//associate inputs a_in_m = mxDuplicateArray(prhs[0]);
//associate pointers a = mxGetPr(a_in_m); int s = (int) a[0];
//associate outputs
c_out_m = plhs[0] = mxCreateDoubleMatrix(s,1,mxREAL);
c = mxGetPr(c_out_m);
comp_routine(c,s);
return;
}
**************************************************************
And these are the errors i receive:
>> mex sdk_vs_gcc.cpp sdk_vs_gcc.cpp sdk_vs_gcc.cpp(6) : error C2057: expected constant expression sdk_vs_gcc.cpp(6) : error C2466: cannot allocate an array of constant size 0 sdk_vs_gcc.cpp(6) : error C2133: 'V' : unknown size
C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: Compile of 'sdk_vs_gcc.cpp' failed.
Thank you for help!

Accepted Answer

Jan
Jan on 10 Sep 2013
I looks like the SDK-Compiler (which version and which SDK?) does not support dynamic allocation. This is possible, because it does not belong to all C++ standard (although I would expect this to work for a long time now).
A workaround is using malloc instead to reserve the memory on the heap, not on the stack:
double *V;
V = (double *) malloc((s-1) * sizeof(double));
...
free(V);
or the corresponding C++ variant with new and delete.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!