[C++] Debuggen gdb

Online-Skater

Erfahrenes Mitglied
Hi Leute,

hoffe es ist diesmal nicht wieder so trivial sondern wirklich fehlendes Wissen :)
C++:
// try to create a form of triangle
Matrix Matrix::toTriangle()
{
    // transforming only by squarish matrix
    if (!this->_square)
    {
        throw std::logic_error("This matrix is not squarish, thatswhy it can't be transform to triangle.");
    }

    if (this->_type == TRIANGULAR_MATRIX)
    {
        return *this;
    }

    Matrix tmp(*this);
    for (usint i = 0; i < _row -1; i++)
    {
        for (usint j = i+1; j < _row; j++)
        {
            double z = -(tmp._arr[j][i]/tmp._arr[i][i]);
            for (usint k = i; k < _col; k++)
            {
                tmp._arr[j][k] += (_arr[i][k] * z);
            }
        }
    }
    tmp._type = TRIANGULAR_MATRIX;

    return tmp;
}

Meine Funktion zum umformen einer quadratischen Matrix in eine Dreiecksform. Wenn ich diese Funktion debugge als Beispiel eine 3*3 Matrix dann bekomme ich ein SEGFAULT Fehlermeldung somit sehe ich nicht den weiteren Verlauf. Ohne Debuggen läuft das Programm wie es soll, nur möchte ich gerne wissen wo der Hase begraben liegt.

mfg
 
Hallo,

könntest du das Problem auf ein Minimalbeispiel erstellen und dann einen Auszug aus der gdb-Session bringen, bei der das Fehlverhalten auftritt?

Grüße,
Matthias
 
Ich weiß nicht genau was du damit meinst aber habe mal direkt im Debugger gestartet.
Code:
(gdb) exec Vectorprog.exe
(gdb) run
Starting program: F:\Projects\C++\Mathe\LGS_obj\bin\Debug/Vectorprog.exe -o Vect
orprog.exe
Loaded symbols for C:\WINDOWS\system32\ntdll.dll
Loaded symbols for C:\WINDOWS\system32\kernel32.dll
Loaded symbols for C:\WINDOWS\system32\msvcrt.dll
--Lineare Gleichungssysteme--

Art der Matrix in der Form [Zeile * Spalte]
Zeilen:3
Spalten:3

Matrixwerte einlesen...
[0][0]: 1
[0][1]: 3
[0][2]: 4
[1][0]: 2
[1][1]: 0
[1][2]: 1
[2][0]: 3
[2][1]: 1
[2][2]: 2

Typ: DEFAULT-Matrix

Program received signal SIGSEGV, Segmentation fault.
0x7c97df51 in ntdll!RtlpNtMakeTemporaryKey ()
   from C:\WINDOWS\system32\ntdll.dll
(gdb)

Keine Ahnung wie man anhand dieser Information etwas vernünftiges rausfinden kann :confused:
 
Ich weiß nicht genau was du damit meinst aber habe mal direkt im Debugger gestartet.
Mit "Minimalbeispiel" meinte ich, dass du den fehlerhaften Teil isolierst und ein (komplettes) minimales Programm bereitstellst, in dem der Fehler auftritt. Ansonsten wird es schwer für uns, das Problem nachzuvollziehen.

Ein Segfault deutet allgemein darauf hin, dass versucht wurde auf einen unreservierten Speicherbereich zuzugreifen. Geh mal Zeile für Zeile durch das Programm und finde raus, an welcher Stelle der Segfault auftritt.
 
Habe vergessen zu erwähnen das es genau beim return auftritt, also wenn ich das Objekt zurückgebe. Meinem Halbwissen nach müsste dieses Objekt auf dem Stack liegen und theoretisch nach dem Sichtbarkeitsbereich zerstört sein. Ob es daran liegt ?
Bei den mathematischen Operationen klappt es aber ohne Probleme ein in der Definition erstelltes Objekt zurückzugeben. Würde ich es als Zeigerobjekt zurückgeben müsste ich mich ja irgendwie noch um die Zerstörung des Objektes kümmern, daher viel zu überzogen.

Wenn diese Informationen nicht reichen mach ich mal ein "Minimalbeispiel"

mfg
 
Habe vergessen zu erwähnen das es genau beim return auftritt, also wenn ich das Objekt zurückgebe. Meinem Halbwissen nach müsste dieses Objekt auf dem Stack liegen und theoretisch nach dem Sichtbarkeitsbereich zerstört sein. Ob es daran liegt ?
Das kommt ganz darauf an. Besitzt die Klasse Matrix Zeiger-Membervariablen? Wenn ja, hast du einen vernünftigen Kopierkonstruktor für die Matrix-Klasse geschrieben? Dieser wird nämlich bei der Rückgabe aufgerufen, da eine Kopie des Objektes erstellt werden muss.
 
Denke schon das der Kopierkonstruktor richtig ist.
Minimalbeispiel:
C++:
// in der main.cpp
try
    {
         // Hauptprogramm
        Matrix A(zeile, spalte);    // zero matrix
        cout << "Matrixwerte einlesen...\n";
        A.fillIn();
        cout << "Determinante: " << A.determinant() << endl;
    }
C++:
// aufgerufene Funktionen
// Standard constructor
Matrix::Matrix(const usint aRow, const usint aCol)
        : _row(aRow), _col(aCol), _precision(2), _square(aRow == aCol), _type(DEFAULT_MATRIX)   // fast assignments
{
    if (_row == 0 or _col == 0)
    {
        throw std::length_error("Matrix::Matrix() - rows and colums must be greater than zero !");
    }

    // allocate memory
    _arr = new double*[_row];
    for (usint i = 0; i < _row; i++)          // rows gets columns
    {
        _arr[i] = new double[_col];
    }

    // set default value => Zero matrix
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] = 0.0;
        }
    }
}

// Copy constructor
Matrix::Matrix(const Matrix& aCopy)
        : _row(aCopy._row), _col(aCopy._col), _precision(aCopy._precision),
        _square(aCopy._square), _type(aCopy._type)
{
    // allocate memory
    _arr = new double*[_row];
    for (usint i = 0; i < _row; i++)
    {
        _arr[i] = new double[_col];
    }

    // fill vector with values
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] = aCopy._arr[i][j];
        }
    }
}

// Fill vector with values
void Matrix::fillIn()
{
    double value(0);
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            std::cout << "[" <<i << "][" << j <<"]: ";
            // try to get a valid floating number
            while (!(std::cin >> value))
            {
                std::cout << "---> Wrong input. Please type only floating numbers ! \n";
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cout << "[" <<i << "][" << j <<"]: ";
            }
            // assign the value
            _arr[i][j] = value;
        }
    }
    std::cout << std::endl;
    // set type
    this->setType();
}

// Try to compute the determinant
const double Matrix::determinant()
{
    // Only squarish matrix have a determinant
    if (!_square)
    {
        throw std::logic_error("This matrix is not squarish, thatswhy they haven't a determinant.");
    }

    double deter;
    if (this->_type == TRIANGULAR_MATRIX)
    {
        deter = _arr[0][0];
        for (usint i = 1; i < _row; i++)
        {
            deter *= _arr[i][i];
        }
    }
    else
    {
        Matrix tmp = this->toTriangle();
        deter = tmp._arr[0][0];
        for (usint i = 1; i < tmp._row; i++)
        {
            deter *= tmp._arr[i][i];
        }
    }

    return deter;
}

// try to create a form of triangle
Matrix Matrix::toTriangle()
{
    // transforming only by squarish matrix
    if (!this->_square)
    {
        throw std::logic_error("This matrix is not squarish, thatswhy it can't be transform to triangle.");
    }

    if (this->_type == TRIANGULAR_MATRIX)
    {
        return *this;
    }

    Matrix tmp(*this);
    for (usint i = 0; i < _row -1; i++)
    {
        for (usint j = i+1; j < _row; j++)
        {
            double z = -(tmp._arr[j][i]/tmp._arr[i][i]);
            for (usint k = i; k < _col; k++)
            {
                tmp._arr[j][k] += (tmp._arr[i][k] * z);
            }
        }
    }
    tmp._type = TRIANGULAR_MATRIX;

    return tmp;
}

Problem nur beim debuggen (Program received signal SIGSEGV, Segmentation fault.)
beim Berechnung der Determinante, welche die Methode toTriangle() aufruft. Und dort kommt die Meldung beim return. Den Kopierkonstruktor kann ich nicht debuggen da springt er irgendwie nicht rein obwohl ich den Breakpoint gesetzt habe.
 
Hallo,

wenn die Unschärfe zuschlägt :).
Bist du dir sicher das er beim return aussteigt? Du kannst dir ja mal den Funktionsstack anschauen indem du in der Debugger-Konsole den Befehl "where" eingibst. Ansonsten, falls der Code überschaubar ist, poste mal die Sourcen zum kompletten Programm.

Gruß,
RedWing
 
Danke erstmal für den Tip, ich werde mir demnächst mal den Umgang mit dem Debugger gdb zu eigen machen denn noch kenne ich mich damit nicht viel aus bzw. nur das was in CodeBlocks integriert ist.

Code:
(gdb) where
#0  0x7c97df51 in ntdll!RtlpNtMakeTemporaryKey ()
   from C:\WINDOWS\system32\ntdll.dll
#1  0x003d0000 in  ()
(gdb)

Das sagt mir garnichts. Da es noch nicht so viel Code ist stell ich ihn auch mal hier rein eventuell fallen euch ja noch mehr Fehler auf. Bin immer dankbar für Verbesserungsvorschläge aller Art, möchte ja schliesslich etwas lernen :)
mymath.h
C++:
#ifndef MYMATH_H
#define MYMATH_H

#include <iostream>         // cout | cin
#include <sstream>          // ostringstream
#include <string>           // string
#include <iomanip>          // setw(n)
#include <stdexcept>        // exceptions
#include <limits>           // numeric_limits
#include <cmath>            // round

typedef unsigned short usint;

enum MatrixType
{
    DEFAULT_MATRIX,
    ROW_MATRIX,
    COL_MATRIX,
    UNIT_MATRIX,
    DIAGONAL_MATRIX,
    TRIANGULAR_MATRIX
};

class Matrix
{
	public:
		Matrix(const usint, const usint);
		Matrix(const Matrix&);
        ~Matrix();

    // Operatoren
        const Matrix&   operator=   (const Matrix&);
        Matrix          operator-   () const;

        const Matrix&   operator+=  (const Matrix&);
        Matrix          operator+   (const Matrix&) const;
        const Matrix&   operator+=  (double);
        Matrix          operator+   (double) const;

        const Matrix&   operator-=  (const Matrix&);
        Matrix          operator-   (const Matrix&) const;
        const Matrix&   operator-=  (double);
        Matrix          operator-   (double) const;

        Matrix          operator*   (const Matrix&) const;
        const Matrix&   operator*=  (double);
        Matrix          operator*   (double) const;

        friend std::ostream& operator<< (std::ostream&, Matrix&);

        // Fill vector with values
        void fillIn();
        // Setting up the precision
        void setPrecision(usint);
        // Try to transform to delta form
        Matrix toTriangle();
        // Try to return the inverse matrix
        Matrix toInverse();
        // Try to compute the determinant
        const double determinant();
        // Get the rang of a matrix
        const usint rang();
        // Get the matrix type as string
        std::string getType();

	protected:

	private:
		usint _row;             // rows
		usint _col;             // columns
		usint _precision;       // determines the precision of the values
        bool _square;           // Flag for quadratic matrix
        MatrixType _type;       // type of matrix
		double** _arr;          // vector


        // Determines the maximum distance between two values in the vector
		usint distance();
		// Set the matrix type
		void setType();
		// Swapping rows
		void swapRows(usint, usint);
};

// Round the value with precision
const double round(const double, const int);

#endif // mymath.h
mymath.cpp
C++:
#include "mymath.h"

/**-------PRIVATE------------------------------------------------*/
// Determines the distance between two values in the vector
usint Matrix::distance()
{
    std::ostringstream out;
    std::string str;
    usint abst = 2;
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            out << round(_arr[i][j], _precision);
            // cast value in a string
            str = out.str();
            usint k = str.length() + 1;
            if (k > abst)
            {
                abst = k;
            }
            out.str("");  // clear stream
            out.clear();  // reset flags
        }
    }

    return abst;
}

// Set the matrix type
void Matrix::setType()
{
    // set default value
    this->_type = DEFAULT_MATRIX;

    // check if matrix is a ROW_MATRIX
    if (_row == 1 and _col > 1)
    {
        this->_type = ROW_MATRIX;
    }
    // check if matrix is a COL_MATRIX
    else if (_col == 1 and _row > 1)
    {
        this->_type = COL_MATRIX;
    }
    // check if matrix is a UNIT-,DIAGONAL- or TRIANGULAR_MATRIX
    else if (_square)
    {
        bool top = true;
        bool down = true;
        bool unit = true;

        for (usint i = 0; i < _row; i++)
        {
            for (usint j = 0; j < _col; j++)
            {
                // check the top-right triangle
                if (j > i)
                {
                    if (_arr[i][j] != 0)
                    {
                        top = false;
                    }
                }

                // check the bottom-left triangle
                if (j < i)
                {
                    if (_arr[i][j] != 0)
                    {
                        down = false;
                    }
                }

                // check the diagonal
                if (i == j)
                {
                    if (_arr[i][j] != 1)
                    {
                        unit = false;
                    }
                }
            }
        }

        // Evaluation
        if (top and down and unit)
        {
            this->_type = UNIT_MATRIX;
        }
        else if (top and down and !unit)
        {
            this->_type = DIAGONAL_MATRIX;
        }
        else if (top xor down)
        {
            this->_type = TRIANGULAR_MATRIX;
        }
    }
}

// Swapping rows
void Matrix::swapRows(usint aRow1, usint aRow2)
{
    for (usint i = 0; i < _col; i++)
    {
        double tmp = _arr[aRow1][i];
        _arr[aRow1][i] = _arr[aRow2][i];
        _arr[aRow2][i] = tmp;
    }
}

/**-------PUBLIC-------------------------------------------------*/

// Standard constructor
Matrix::Matrix(const usint aRow, const usint aCol)
        : _row(aRow), _col(aCol), _precision(2), _square(aRow == aCol), _type(DEFAULT_MATRIX)   // fast assignments
{
    if (_row == 0 or _col == 0)
    {
        throw std::length_error("Matrix::Matrix() - rows and colums must be greater than zero !");
    }

    // allocate memory
    _arr = new double*[_row];
    for (usint i = 0; i < _row; i++)          // rows gets columns
    {
        _arr[i] = new double[_col];
    }

    // set default value => Zero matrix
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] = 0.0;
        }
    }
}

// Copy constructor
Matrix::Matrix(const Matrix& aCopy)
        : _row(aCopy._row), _col(aCopy._col), _precision(aCopy._precision),
        _square(aCopy._square), _type(aCopy._type)
{
    // allocate memory
    _arr = new double*[_row];
    for (usint i = 0; i < _row; i++)
    {
        _arr[i] = new double[_col];
    }

    // fill vector with values
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] = aCopy._arr[i][j];
        }
    }
}

// Destructor
Matrix::~Matrix()
{
    // deallocate memory
    for (usint i = 0; i < _row; i++)
    {
        delete[] _arr[_row];
    }
    delete[] _arr;
}

/**------- Public methods----------------------------------------*/

// Fill vector with values
void Matrix::fillIn()
{
    double value(0);
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            std::cout << "[" <<i << "][" << j <<"]: ";
            // try to get a valid floating number
            while (!(std::cin >> value))
            {
                std::cout << "---> Wrong input. Please type only floating numbers ! \n";
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cout << "[" <<i << "][" << j <<"]: ";
            }
            // assign the value
            _arr[i][j] = value;
        }
    }
    std::cout << std::endl;
    // set type
    this->setType();
}

void Matrix::setPrecision(usint aVal)
{
    _precision = aVal;
}

/**-------OPERATORS----------------------------------------------*/
//------------------- Assignment
const Matrix& Matrix::operator= (const Matrix& aRef)
{
    if (this != &aRef)   // Verhindert Selbstkopie
    {
        // call the destructor
        this->~Matrix();

        // allocate memory
        _arr = new double*[_row];
        for (usint i = 0; i < _row; i++)
        {
            _arr[i] = new double[_col];
        }

        // copy values
        for (usint i = 0; i < _row; i++)
        {
            for (usint j = 0; j < _col; j++)
            {
                _arr[i][j] = aRef._arr[i][j];
            }
        }
    }
    // return reference
    return *this;
}

//------------------- Negation
Matrix Matrix::operator- () const
{
    // call copy constructor
    Matrix temp(*this);
    // negate each value
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            temp._arr[i][j] = -temp._arr[i][j];
        }
    }
    // return copy
    return temp;
}

//------------------- Operator to add two vectors together
const Matrix& Matrix::operator+= (const Matrix& aRef)
{
    // vectors must have same structure
    if (_row != aRef._row or _col != aRef._col)
        throw std::length_error("Matrix::operator+=");
    // add
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] += aRef._arr[i][j];
        }
    }
    // return reference
    return *this;
}

Matrix Matrix::operator+ (const Matrix& aRef) const
{
    // vectors must have same structure
    if (_row != aRef._row or _col != aRef._col)
        throw std::length_error("Matrix::operator+");

    Matrix temp(*this);
    temp += aRef;

    return temp;
}

const Matrix& Matrix::operator+= (double aVal)
{
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] += aVal;
        }
    }

    return *this;
}

Matrix Matrix::operator+ (double aVal) const
{
    Matrix temp(*this);
    temp += aVal;

    return temp;
}

//------------------- Operator for subtract two vectors
const Matrix& Matrix::operator-= (const Matrix& aRef)
{
    // vectors must have same structure
    if (_row != aRef._row or _col != aRef._col)
        throw std::length_error("Matrix::operator-=");
    // subtract
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] -= aRef._arr[i][j];
        }
    }
    // return reference
    return *this;
}

Matrix Matrix::operator- (const Matrix& aRef) const
{
    // vectors must have same structure
    if (_row != aRef._row or _col != aRef._col)
        throw std::length_error("Matrix::operator-");

    Matrix temp(*this);
    temp -= aRef;

    return temp;
}

const Matrix& Matrix::operator-= (double aVal)
{
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] -= aVal;
        }
    }

    return *this;
}

Matrix Matrix::operator- (double aVal) const
{
    Matrix temp(*this);
    temp -= aVal;

    return temp;
}

//--------------- Multiplication with "Falkschen Schema"
Matrix Matrix::operator* (const Matrix& aRef) const
{
    // vectors must have special structure
    if (_col != aRef._row)
        throw std::length_error("Matrix::operator*");

    // create temporary object
    Matrix temp(_row, aRef._col);
    for (usint i = 0; i < aRef._col; i++)
    {
        for (usint j = 0; j < _row; j++)
        {
            for (usint k = 0; k < _col; k++)
            {
                temp._arr[j][i] += (_arr[j][k] * aRef._arr[k][i]);
            }
        }
    }
    // return object
    return temp;
}

const Matrix& Matrix::operator*= (double aVal)
{
    for (usint i = 0; i < _row; i++)
    {
        for (usint j = 0; j < _col; j++)
        {
            _arr[i][j] *= aVal;
        }
    }

    return *this;
}

Matrix Matrix::operator* (double aVal) const
{
    Matrix temp(*this);
    temp *= aVal;

    return temp;
}

// Overloading the output operator
std::ostream& operator<< (std::ostream& aOut, Matrix& aRef)
{
    // Abstand ermitteln [abst]
    usint abst = aRef.distance();
    for (usint i = 0; i < aRef._row; i++)
    {
        aOut << "|";
        for (usint j = 0; j < aRef._col; j++)
        {
            aOut << std::setw(abst) << round(aRef._arr[i][j], aRef._precision);
        }
        aOut << " |\n";
    }
    aOut << std::endl;
    return aOut;
}

/**-------Special functions--------------------------------------------*/
// try to create a form of triangle
Matrix Matrix::toTriangle()
{
    // transforming only by squarish matrix
    if (!this->_square)
    {
        throw std::logic_error("This matrix is not squarish, thatswhy it can't be transform to triangle.");
    }

    if (this->_type == TRIANGULAR_MATRIX)
    {
        return *this;
    }

    Matrix tmp(*this);
    for (usint i = 0; i < _row -1; i++)
    {
        for (usint j = i+1; j < _row; j++)
        {
            if (tmp._arr[i][i] == 0)
            {
                tmp.swapRows(i,j);
            }
            double z = -(tmp._arr[j][i]/tmp._arr[i][i]);
            for (usint k = i; k < _col; k++)
            {
                tmp._arr[j][k] += (tmp._arr[i][k] * z);
            }
        }
    }
    tmp._type = TRIANGULAR_MATRIX;

    return tmp;
}

// Try to compute the determinant
const double Matrix::determinant()
{
    // Only squarish matrix have a determinant
    if (!_square)
    {
        throw std::logic_error("This matrix is not squarish, thatswhy they haven't a determinant.");
    }

    double deter;
    if (this->_type == TRIANGULAR_MATRIX)
    {
        deter = _arr[0][0];
        for (usint i = 1; i < _row; i++)
        {
            deter *= _arr[i][i];
        }
    }
    else
    {
        Matrix tmp = this->toTriangle();
        deter = tmp._arr[0][0];
        for (usint i = 1; i < tmp._row; i++)
        {
            deter *= tmp._arr[i][i];
        }
    }

    return deter;
}

// Get the rang of a matrix
const usint Matrix::rang()
{
    if (!_square)
    {
        throw std::logic_error("This program can only compute the rang of a squarish matrix.");
    }

    Matrix tmp = this->toTriangle();
    usint rg = 0;
    for (usint i = 0; i < tmp._row; i++)
    {
        double count = 0;
        for (usint j = 0; j < tmp._col; j++)
        {
            count += tmp._arr[i][j];
        }

        if (count != 0.0)
        {
            rg++;
        }
    }

    return rg;
}

// Try to transform to inverse matrix with "adjunkte"
Matrix Matrix::toInverse()
{
    // if matrix have no square format then dont exits the inverse
    if (!_square)
    {
        throw std::logic_error("This matrix is not squarish, thatswhy it haven't an inverse matrix.");
    }

    const double det = this->determinant();
    if (!det)
    {
        throw std::logic_error("The determinant is zero so it dont't exists an inverse matrix.");
    }

    // create inverse matrix
    Matrix inverseMatrix(_row, _col);

    // create temporary matrix for computing the complementary matrix
    usint size = _row - 1;
    Matrix tmp(size, size);

    // loop for every row
    for (usint i = 0; i < _row; i++)
    {
        // loop for every column
        for (usint j = 0; j < _col; j++)
        {
            // position counter for collected values (which are not striked)
            usint x = 0, y = 0;
            // interior loop for striking row and column
            for (usint k = 0; k < _row; k++)
            {
                for (usint l = 0; l < _col; l++)
                {
                    if (i != k and j != l)
                    {
                        tmp._arr[x][y] = this->_arr[k][l];
                        y++;
                        if (y == size)
                        {
                            y = 0;
                            x++;
                        }
                    }
                }
            }
            // complementary matrix value
            inverseMatrix._arr[j][i] = (std::pow(-1.0, j+i) * tmp.determinant()) / det;
        }
    }

    return inverseMatrix;
}

// Get the matrix type as string
std::string Matrix::getType()
{
    switch(_type)
    {
        case ROW_MATRIX:        return "ROW-Matrix";
        case COL_MATRIX:        return "COLUMN-Matrix";
        case UNIT_MATRIX:       return "UNIT-Matrix";
        case DIAGONAL_MATRIX:   return "DIAGONAL-Marix";
        case TRIANGULAR_MATRIX: return "TRIANGULAR-Matrix";
        default:                return "DEFAULT-Matrix";
    }
}

///--------------------- extern function---------------------------
// Round the value with precision
const double round(const double value, const int precision)
{
    const double multiplier(std::pow(10.0, precision));
    return std::floor(value * multiplier + 0.5) / multiplier;
}
main.cpp (Testumgebung) -> wird noch zum richtigen Programm ausgebaut
C++:
#include "mymath.h"

using namespace std;

int main()
{
  cout << "--Lineare Gleichungssysteme--\n\n";
  cout << "Art der Matrix in der Form [Zeile * Spalte]\n";
  usint zeile, spalte;
  if (cout << "Zeilen:" and cin >> zeile and cout << "Spalten:" and cin >> spalte)
  {
    try
    {
         // Hauptprogramm
        cout << endl;
        Matrix A(zeile, spalte);    // zero matrix
        cout << "Matrixwerte einlesen...\n";
        A.fillIn();
        cout << "Determinante: " << A.determinant() << endl;
        cout << "Rang: " << A.rang() << endl;
        cout << "Inverse Matrix:" << endl;
        Matrix B = A.toInverse();
        cout << B;
    }
    catch (std::length_error &e)
    {
        cerr << typeid(e).name() << ": " << e.what() << endl;
    }
    catch (std::logic_error &e)
    {
        cerr << typeid(e).name() << ": " << e.what() << endl;
    }
    catch (...)
    {
        cerr << "Unknown error occured";
    }
  }
  else cerr << "Zeilen und Spalten sind positive Werte > 0 !";

  return 0;
}


mfg ;-)
 
Hallo,

dein Fehler liegt im Destruktor:

C++:
     // Destructor
       Matrix::~Matrix()
       {
           // deallocate memory
           for (usint i = 0; i < _row; i++)
           {
               delete[] _arr[row];
           }
           delete[] _arr;
       }
sollte wahrsch heißen:

C++:
       // Destructor
       Matrix::~Matrix()
       {
           // deallocate memory
           for (usint i = 0; i < _row; i++)
           {
               delete[] _arr[i];
           }
           delete[] _arr;
       }

Btw: laut deiner Ausgabe ist dein Programm nicht mit Debugsymbolen übersetzt ansonsten würde der gdb auch die komplette Spur im Funktionsstacktrace ausgeben.
So sieht das bei mir aus:
Code:
(gdb) where
#0  0xb7da06f9 in free () from /lib/tls/i686/cmov/libc.so.6
#1  0xb7f4c3b1 in operator delete () from /usr/lib/libstdc++.so.6
#2  0xb7f4c40d in operator delete[] () from /usr/lib/libstdc++.so.6
#3  0x0804a639 in ~Matrix (this=0xbfdef4e0) at mymath.cpp:169
#4  0x0804b801 in Matrix::toTriangle (this=0xbfdef5e0) at mymath.cpp:450
#5  0x0804b978 in Matrix::determinant (this=0xbfdef5e0) at mymath.cpp:473
#6  0x0804953f in main () at main.cpp:19

Gruß,
RedWing
 
Zurück