微控制器在malloc失败

前端之家收集整理的这篇文章主要介绍了微控制器在malloc失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在32位cortex m4微控制器上编写函数.
函数必须能够乘以不同大小的矩阵,这是我无法预测的.所以我必须使用malloc ……

但我不明白为什么我的mc在执行以下行时总是进入默认处理程序中断:

  1. double *output2=NULL;
  2. output2 = malloc(3 *1* sizeof(double *));

这个mc是不是能够处理这种类型的操作?
虽然这在我的笔记本电脑上工作得很好!

**编辑*

这里有一些代码(仍然需要修改……):
好吧所有的malocs都失败了.我无法为“malloced”数组赋值.

  1. int main (void)
  2. {
  3.  
  4. /*some stuff*/
  5.  
  6. float transFRotMatrix[3][3]={0}; //array gets modified by other functions
  7. float sunMeasurements[3][1] = {{1},{2},{3}}; //test values
  8.  
  9. multiplyMatrices( &transFRotMatrix[0][0],3,&sunMeasurements[0][0],1,*orbitalSunVector);
  10.  
  11. /*some stuff*/
  12. }
  13.  
  14. void multiplyMatrices(float *transposedMatrix,int height1,int width1,float *iSunVector,int height2,int width2,float *orbitalSunVector)
  15. {
  16.  
  17. int y=0;
  18. int x = 0;
  19. int row=0;
  20. int column =0;
  21. int k=0;
  22. int k2 = 0;
  23. float result = 0;
  24.  
  25. int i=0;
  26. int j=0;
  27. int t=0;
  28.  
  29. float rotationMatrix[3][3]={0};
  30.  
  31. i=0;
  32. k=0;
  33. k2 = 0;
  34.  
  35.  
  36. if(width1 != height2)
  37. {
  38. printf("unmatching matrices,error.\n\n");
  39. return;
  40. }
  41.  
  42. float *output2;
  43.  
  44. output2 = malloc(3 *1* sizeof(float *)); //<-----ERROR
  45.  
  46.  
  47. while(k<width1) //aantal rijen 1ste matrix
  48. {
  49. for(j=0;j<height2;j++) //aantal rijen 2de matrix
  50. {
  51. result += (*((transposedMatrix+k*width1)+j)) * (*((iSunVector+j*width2)+k2)); //1ste var:aantal kolommen 2de matrix --2de variabele na de plus = aantal kolommen 2de matrix
  52. //printf("%f * %f\t + ",(*((transposedMatrix+k*width1)+j)),(*((iSunVector+j*width2)+k2)));
  53. }
  54.  
  55. output2[k*3 +k2] = result; //<-----FAILS HERE
  56.  
  57.  
  58. k2++;
  59. x++;
  60. column++;
  61.  
  62. if(x==width2)
  63. {
  64. k2=0;
  65. x=0;
  66. column=0;
  67. row++;
  68. y++;
  69. k++;
  70.  
  71. }
  72. result = 0;
  73.  
  74. }
  75.  
  76. for(i=0;i<height1;i++)
  77. {
  78. for(j=0;j<width2;j++)
  79. {
  80. orbitalSunVector[j * height1 + i] = output2[i*3 +j];
  81. }
  82. }
  83. free(output2);
  84. }

解决方法

malloc()在此代码的其他部分工作正常吗?在编写嵌入式设备时,堆可能没有正确初始化,或者已经为另一个自定义的malloc()函数初始化了.

猜你在找的C&C++相关文章