Ich habe ein Problem bei der Zuweisung einer Struktur in einer Funktion. Hier ist der Code (ich benutze derzeit Visual Studio 2008):Ausdruck kann nicht ausgewertet werden. Malloc fehlgeschlagen
Mat3x3* ProdMat(Mat3x3 *m, Mat3x3 *n)
{
if(m == NULL || n == NULL)
{
cout << "\t[W] Cannot compute product of the two matrixes one or both are NULL." << endl;
return NULL;
}
Mat3x3 *p; // product
int i, j;
float sum = 0;
p = (Mat3x3*)malloc(sizeof(Mat3x3)); // <= Exp cannot be evaluated
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
sum = 0;
for(int k = 0; k < 3; k++)
{
float a = m->a[i][k];
float b = n->a[k][j];
sum += a * b;
}
p->a[i][j] = sum;
}
}
return p;
}
P enthält eine Matrix mit 9 Einträgen. Hier ist der Kontext, in dem der Fehler gegeben:
Mat3x3* compute_final_trans(Trans **transes) // compute product of all transformation matrixes from right to left
{
int k_trans = 0, i, j;
Mat3x3 *final_trans;
if(transes == NULL)
{
printf("\t[E] Cannot compute sequence of NULL transformations.\n");
return NULL;
}
final_trans = (Mat3x3*)malloc(sizeof(final_trans));
for(i = 0; i < 3; i++) // generate eye matrix
for(j = 0; j < 3; j++)
{
if(i == j)
{
final_trans->a[i][j] = 1;
}
else
{
final_trans->a[i][j] = 0;
}
}
while(transes[k_trans++]);
for(i = k_trans - 2; i >= 0; i--)
{
final_trans = ProdMat(transes[i]->matrix, final_trans); // <= ERROR
}
return final_trans;
}
Schluss trans wie das Auge Matrix initialisiert und Transes wurden erfolgreich vor diesem Schritt berechnet (vor compute_final_trans Aufruf). Die while wird verwendet, um die Anzahl der Transformationen, die transes enthält, zu verringern. In Zeile:
final_trans = ProdMat(transes[i]->matrix, final_trans);
ProdMat ausfällt Speicher für P zuzuteilen, die ein Zeiger auf eine Mat3x3 Struktur ist. perror schlägt vor, dass nicht genügend Speicher für die Struktur vorhanden ist. Allerdings verwende ich nur 1 GB RAM (4 GB insgesamt).
Jede Hilfe/Anregung/Referenz wird sehr geschätzt.
Sebi
Soll das C oder C++ oder etwas anderes sein? –
Es soll ANSI C sein, obwohl ich mehr Erfahrung mit C++ habe. – Sebi
OK - Ich habe dies jetzt als C markiert, um weitere Verwirrung zu vermeiden –