【发布时间】:2015-07-05 14:32:59
【问题描述】:
我正在 32 位 cortex m4 微控制器上编写函数。 该函数必须能够乘以不同大小的矩阵,这是我无法预测的。所以我必须使用 malloc...
但我不明白为什么我的 mc 在执行以下行时总是进入默认处理程序中断:
double *output2=NULL;
output2 = malloc(3 *1* sizeof(double *));
这个 mc 不能处理这种类型的操作吗? 虽然这在我的笔记本电脑上运行良好!
**编辑*
这里还有一些代码(仍然需要修改......): 好吧,任何地方的所有maloc都失败了。我无法为“malloced”数组分配任何值。
int main (void)
{
/*some stuff*/
float transFRotMatrix[3][3]={0}; //array gets modified by other functions
float sunMeasurements[3][1] = {{1},{2},{3}}; //test values
multiplyMatrices( &transFRotMatrix[0][0],3, 3, &sunMeasurements[0][0], 3, 1, *orbitalSunVector);
/*some stuff*/
}
void multiplyMatrices(float *transposedMatrix, int height1, int width1, float *iSunVector,int height2, int width2, float *orbitalSunVector)
{
int y=0;
int x = 0;
int row=0;
int column =0;
int k=0;
int k2 = 0;
float result = 0;
int i=0;
int j=0;
int t=0;
float rotationMatrix[3][3]={0};
i=0;
k=0;
k2 = 0;
if(width1 != height2)
{
printf("unmatching matrices, error.\n\n");
return;
}
float *output2;
output2 = malloc(3 *1* sizeof(float *)); //<-----ERROR
while(k<width1) //aantal rijen 1ste matrix
{
for(j=0;j<height2;j++) //aantal rijen 2de matrix
{
result += (*((transposedMatrix+k*width1)+j)) * (*((iSunVector+j*width2)+k2)); //1ste var:aantal kolommen 2de matrix --2de variabele na de plus = aantal kolommen 2de matrix
//printf("%f * %f\t + ", (*((transposedMatrix+k*width1)+j)), (*((iSunVector+j*width2)+k2)));
}
output2[k*3 +k2] = result; //<-----FAILS HERE
k2++;
x++;
column++;
if(x==width2)
{
k2=0;
x=0;
column=0;
row++;
y++;
k++;
}
result = 0;
}
for(i=0;i<height1;i++)
{
for(j=0;j<width2;j++)
{
orbitalSunVector[j * height1 + i] = output2[i*3 +j];
}
}
free(output2);
}
【问题讨论】:
-
可能他不支持浮点数?
-
最好像@Zelldon 建议的那样隔离浮点问题。如果你 malloc(20) 进入 int* 会发生什么?
-
您是否 100% 确定是 malloc 调用失败,而不是其后的某些代码? Afaik 你应该分配
3 * sizeof( double )(至少如果我理解正确的话——由于你使用空格的方式,你的代码很难阅读)——这可能是3 * sizeof( double* )的两倍,所以你可能正在访问某处数据越界 -
@trilolil 如果你想要双精度数组,那么
sizeof(double*)肯定是错误的。也许您需要发布更多代码。 -
别人指出
output2 = malloc(3 *1* sizeof(double *));是错误的,应该是output2 = malloc(3 * 1 * sizeof *output2);分配3x1doubles。通过取消引用output2指针,我们不需要重复类型名称,并且如果output2被重命名或删除,则会出现错误。不过,此修复无法解决您看到的错误。
标签: c pointers malloc microcontroller