微控制器在 malloc 失败
microcontroller fails at malloc
我正在 32 位 cortex m4 微控制器上编写函数。
该函数必须能够乘以不同大小的矩阵,这是我无法预测的。所以我必须使用 malloc...
但我不明白为什么我的 mc 在执行以下行时总是进入默认处理程序中断:
double *output2=NULL;
output2 = malloc(3 *1* sizeof(double *));
难道这个mc不能处理这种操作吗?
虽然这在我的笔记本电脑上运行得很好!
**编辑*
这里再添加一些代码(仍然需要修改...):
好吧,任何地方的所有 malocs 都会失败。我无法为 "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);
}
C 中有两种有效的实现类型:hosted 和 freestanding。为托管实现编写的程序可以使用任何标准库头文件。但是,独立实现只需要支持 <float.h>
、<iso646.h>
、<limits.h>
、<stdalign.h>
、<stdarg.h>
、<stdbool.h>
、<stddef.h>
, <stdint.h>
, 和 <stdnoreturn.h>
.
微控制器的支持很少,因此您的 C 编译器(或者更确切地说,标准库)可能不完全支持 <stdlib.h>
(因此 malloc
)。但是,在不知道您选择使用哪个编译器和标准库的情况下,这只是一个猜测。
正如您的问题的评论所暗示的,也有可能是这段代码之前(and/or 之后)的原因。也许你可以制作一个 mcve?
当您更新以提供更多信息时,请随时在此答案下方发表评论,我将很乐意详细说明以提供更多信息。
您收到的是处理器异常,而不是中断。
它们都映射到同一个处理程序,这是一种常见的做法。
我假设你有硬件故障。
您可以检查寄存器以查看导致异常的原因。
使用 this code 作为参考来检查它们。
如果malloc内部出现异常,可能与libc实现有关。
malloc()
在此代码的其他部分工作正常吗?当您对嵌入式设备进行编程时,堆可能未正确初始化,或者已针对另一个自定义 malloc()
类函数进行了初始化。
我正在 32 位 cortex m4 微控制器上编写函数。 该函数必须能够乘以不同大小的矩阵,这是我无法预测的。所以我必须使用 malloc...
但我不明白为什么我的 mc 在执行以下行时总是进入默认处理程序中断:
double *output2=NULL;
output2 = malloc(3 *1* sizeof(double *));
难道这个mc不能处理这种操作吗? 虽然这在我的笔记本电脑上运行得很好!
**编辑*
这里再添加一些代码(仍然需要修改...): 好吧,任何地方的所有 malocs 都会失败。我无法为 "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);
}
C 中有两种有效的实现类型:hosted 和 freestanding。为托管实现编写的程序可以使用任何标准库头文件。但是,独立实现只需要支持 <float.h>
、<iso646.h>
、<limits.h>
、<stdalign.h>
、<stdarg.h>
、<stdbool.h>
、<stddef.h>
, <stdint.h>
, 和 <stdnoreturn.h>
.
微控制器的支持很少,因此您的 C 编译器(或者更确切地说,标准库)可能不完全支持 <stdlib.h>
(因此 malloc
)。但是,在不知道您选择使用哪个编译器和标准库的情况下,这只是一个猜测。
正如您的问题的评论所暗示的,也有可能是这段代码之前(and/or 之后)的原因。也许你可以制作一个 mcve?
当您更新以提供更多信息时,请随时在此答案下方发表评论,我将很乐意详细说明以提供更多信息。
您收到的是处理器异常,而不是中断。 它们都映射到同一个处理程序,这是一种常见的做法。 我假设你有硬件故障。 您可以检查寄存器以查看导致异常的原因。 使用 this code 作为参考来检查它们。 如果malloc内部出现异常,可能与libc实现有关。
malloc()
在此代码的其他部分工作正常吗?当您对嵌入式设备进行编程时,堆可能未正确初始化,或者已针对另一个自定义 malloc()
类函数进行了初始化。