Ich schreibe eine Funktion auf einem 32-Bit-Cortex-M4-Mikrocontroller. Die Funktion muss in der Lage sein, Matrizen unterschiedlicher Größe zu multiplizieren, was ich nicht vorhersagen kann. So habe ich eine malloc verwenden ...Mikrocontroller schlägt bei malloc
Aber ich verstehe nicht, warum mein mc immer in Interrupt-Standard-Handler geht, wenn sie folgende Zeile ausführt:
double *output2=NULL;
output2 = malloc(3 *1* sizeof(double *));
Ist das nicht in der Lage mc zu handhaben Art der Operationen? Während das auf meinem Laptop einwandfrei funktioniert!
** EDIT *
Hier von etwas mehr Code (die noch geändert werden muss ...): Nun alle malocs überall scheitern. Ich kann keinem "Malloced" -Array einen Wert zuweisen.
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);
}
vielleicht unterstützt er nicht Fließkomma? – Zelldon
Am besten isolieren Sie das Fließkomma-Problem wie es @Zelldon vorschlägt. Was passiert wenn du malloc (20) in ein int * machst? –
Sind Sie 100% sicher, dass es der Malloc-Aufruf ist, der fehlschlägt, und nicht irgendein Code danach?Afaik sollten Sie '3 * sizeof (double)' zuweisen (zumindest wenn ich es richtig verstehe - Ihr Code ist sehr schwer zu lesen aufgrund der Art, wie Sie Leerzeichen verwenden) - das ist wahrscheinlich doppelt so viele Bytes wie '3 * sizeof (double *) ', damit Sie irgendwo außerhalb der Grenzen auf Daten zugreifen können. – stijn