// Gaus.cpp: определяет точку входа для консольного приложения. // #include "stdafx.h" #include <iostream> #include <fstream> using namespace std; class matrix { private: int n; double **A; double *b; inline void ifend(); void filling(); public: matrix() :n(0), A(NULL), b(NULL) {} matrix(int x) :n(x) { b = new double[n]; A = new double *[n]; for (int i(0); i < n; i++) { *A = new double[n]; } } void constructor(); void gauss(); void show(); }; inline void matrix::ifend() //Проверка на заполнение размера матрицы { if (n == 0) { cout << "The size of matrix not found"; system("pause"); exit(0); } } void matrix::constructor() //Создание системы уравнений { matrix::ifend(); matrix::filling(); } void matrix::filling() //Заполнение расширенного массива { matrix::ifend(); for (int i(0); i < n; i++) { system("pause"); for (int j(0); j < n; j++) { cout << "a" << i + 1 << j + 1 << " = "; system("pause"); cin >> A[i][j]; system("pause"); } cout << "b" << i << " = "; cin >> b[i]; } } void matrix::show() { matrix::ifend(); for (int i(0); i < n; i++) { for (int j(0); j < n; j++) { cout << A[i][j] << " * x" << j + 1; if (j == n - 1) cout << " = " << b[i]; else cout << " + "; } cout << endl; } } int main() { matrix obj1(3); obj1.constructor(); system("pause"); return 0; } 

It turns out an error when filling in A [2] [1] i. in the second line of the array. I somehow incorrectly allocate memory for a two-dimensional array? The first line is filled with norms.

  • You have a memory leak (there is no destructor and the memory is not released from under the matrix) and there is a method called "constructor", although it is an initializer. - free_ze

2 answers 2

This

  for (int i(0); i < n; i++) { *A = new double[n]; } 

change to

  for (int i(0); i < n; i++) { A[i] = new double[n]; } 

And then you assign all the memory for the first row only ...

    In this loop in the constructor

      for (int i(0); i < n; i++) { *A = new double[n]; } 

    there is a memory leak, because at each iteration of the loop the address of the allocated memory is overwritten in the variable *A , or, which is the same, in A[0] .

    Correctly write the cycle as

      for (int i(0); i < n; i++) { A[i] = new double[n]; } 

    Please note that you have duplicated ifend function ifend

     void matrix::constructor() //Создание системы уравнений { matrix::ifend(); ^^^^^^^^^^^^^^^ matrix::filling(); } void matrix::filling() //Заполнение расширенного массива { matrix::ifend(); ^^^^^^^^^^^^^^^ 

    You can remove this call from the filling function.