C CODE Generation - Why does my code not terminate?

4 次查看(过去 30 天)
Hello,
I generated some C code with the Matlab coder. I wrote a main script to run the functions I generated. I dont get any error messages while running my code. However the code wont terminate. I know this question is possibly very diffcult to ask, but did someone face similar difficulties and what are possible reasons that a generated code wont terminate while I did not had any problems with the orignal code in MATLAB? I generated more then 30 functions. I dont think, its a good idea to attach all of them here.
This is my main script:
void main(void)
{
static double x_data[1024];
double Results[18];
double dv[6];
double maxP_tmp;
int Fs;
double tol;
int x_size[1];
int i;
int j;
/* Initialize function 'EV_Code' input arguments. */
/* Initialize function input argument 'x'. */
for (i = 0; i <1024; i++) {
x_data[i] = 1;
}
x_data[3] = 1.2;
x_data[230] = 1.3;
x_data[443] = 1.7;
x_data[354] = 1.9;
x_data[146] = 1.6;
x_size[0]= 2;
maxP_tmp = 1.1;
dv[0] = 114.5;
dv[1] = 90.81;
dv[2] = 73.72;
dv[3] = 87.76;
dv[4] = 72.78;
dv[5] = 59.65;
Fs = 1000;
tol = 0.04;
EV_Code_X(x_data, x_size, maxP_tmp, Fs, dv, tol, Results);
EV_Code_X_terminate();
// return 0;
}
  8 个评论
HF
HF 2021-2-11
I paused the simulation after 1 000 000 and 2 000 000 instructions. The program was still executing this particular line.
That is the reason why I am quite confused because I had implemented a condition of maximum number of iteration for the while loop.
Rik
Rik 2021-3-8
I have restored the original question body from the Google cache. Don't remove it again.

请先登录,再进行评论。

回答(1 个)

Geoff Hayes
Geoff Hayes 2021-2-12
编辑:Walter Roberson 2021-3-8
HF - the code that you mention
for (i = 0; i <= coffset; i++) {
Xm0_data[i] = Xm0->data[(aoffset + 30 * i) - 1];
}
occurs before the while loop so the while isn't the problem. In this case, what is coffset initialized to? Is it a valid or reasonable integer? It seems to get initialized via Xm0 but all I see for this variable is that it is defined as
emxArray_real_T *Xm0;
and later initialized as
emxInit_real_T(&Xm0, 2);
but is that sufficient for
i = Xm0->size[0] * Xm0->size[1];
Xm0->size[0] = 30;
Xm0->size[1] = x_size[0] + 29;
emxEnsureCapacity_real_T(Xm0, i);
? Since i is being initialized before the size, I wonder if that is somehow corrupting the object.
  30 个评论
Geoff Hayes
Geoff Hayes 2021-2-12
The passing of i in emxEnsureCapacity_real_T(Xm0, i) may be irrelevant because of
if (emxArray->data != NULL) {
and this should be null because it is initialized to null in the other function at
emxArray->data = (double *)NULL;
So there is no data to copy into the newly allocated array, so this parameter of i is unimportant.
But in this capacity function, look at this code
newNumel = 1;
for (i = 0; i < emxArray->numDimensions; i++) {
newNumel *= emxArray->size[i];
}
if (newNumel > emxArray->allocatedSize) {
i = emxArray->allocatedSize;
if (i < 16) {
i = 16;
}
while (i < newNumel) {
if (i > 1073741823) {
i = MAX_int32_T;
} else {
i *= 2;
}
}
newData = calloc((unsigned int)i, sizeof(double));
Given what we already know, newNumel should be 30*31=930. The emxArray->allocatedSize is 0 so i is initialized to 16. Then in the while loop we multiply i by 2 on each iteration until it is greater than newNumel and so should be initialized to 1024 which you should be able to confirm with Xm0->allocatedSize. So you should have enough memory allocated in your array. Can you confirm that this allocatedSize is 1024?
Geoff Hayes
Geoff Hayes 2021-2-14
Why do you think that the program skipped the call to emxEnsureCapacity_real_T? Check the Xm0->allocatedSize to see what this value is. (I don't have the MATLAB coder so cannot test out your files.)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by