Problem with Dynamic Memory in QT

0

I have a problem with dynamic memories. I have this header, which I'm calling with my main.cpp , the Void of Multiplying does not work for me, and the one of Adding because the process stops.

I do not know much about how to reserve memory and how to release it, and from what I know I think it does not release it, since sometimes it executes the code but my program ends up cycled.

#ifndef MATRICES
#define MATRICES
#include <stdio.h>

class cMatriz{
public:                                            //declarar directivas publicas
    cMatriz(void){}                              //declarar constructor vacio
    cMatriz(int Filas_, int Columnas_){
        Filas = Filas_;
        Columnas = Columnas_;

    //  Constructor  Matriz
    Matriz=new double*[Filas];                  //Reserva memoria para las filas
         for(i=0;i<Filas;i++){
            Matriz[i]=new double[Columnas];         //Reserva memoria para las columas de cada fila
            for(j=0;j<Columnas;j++)                  //Coloca ceros
                Matriz[i][j]=0.0;
            }

    }

    //Destructor
   ~cMatriz(void){
        for(i=0;i<Filas;i++)                        //Libera memoria para las columnas de cada fila
            delete Matriz[i];
        delete Matriz;                              //Libera memoria de las filas
    }

    void GuardaEn(int i, int j, double Valor){
        Matriz[i][j]=Valor;
    }

    void Imprime(void){                             //Imprime la matriz
        printf("\n\n");
        for(i=0;i<Filas;i++){
            for(j=0;j<Columnas;j++)
                 printf("%f\t",Matriz[i][j]);
                 printf("\n");
            }
    }

    void Ceros(void){                               //Rellena la matriz con ceros
        for(i=0;i<Filas;i++){
            for(j=0;j<Columnas;j++)
                 Matriz[i][j]=0.0;
            }
    }

    void Unos(void){                                //Rellena la matriz con unos
        for(i=0;i<Filas;i++){
            for(j=0;j<Columnas;j++)
                 Matriz[i][j]=1.0;
            }
    }

    void Identidad(void){                           //Crea la matriz identidad
        for(i=0;i<Filas;i++){
            for(j=0;j<Columnas;j++){
                 Matriz[i][j]=( i == j ? 1.0 : 0.0);

            }
            }
    }

     void Llenar(void){                                 //Llenar matriz a travez de teclado
         for  (i=0;i<Filas;i++){
            for  (j=0;j<Columnas;j++){
                 printf("Ingrese la posicion %d", i+1);
                 printf("%d ", j+1);
                 printf(" de la Matriz\t");
                 scanf("%lf",&Matriz[i][j]);
            }
         }
     }

     void Aleatoria(void){                                 //Llenar matriz aleatoriamente
         for  (i=0;i<Filas;i++){
            for  (j=0;j<Columnas;j++){
                Matriz[i][j] = rand() % 30;
            }
         }
     }


    void MulEscal(void){                            //Multilicacion por un Escalar
        //k=0;
        printf("Ingrese el escalar a multiplicar. ");
        scanf("%d",&k);
        for  (i=0;i<Filas;i++){
            for  (j=0;j<Columnas;j++){
                 Matriz[i][j]=Matriz[i][j]*k;
            }
         }
    }

    void Transpuesta(cMatriz A){                                 //Calcular transpuesta
        for(i=0;i<Filas;i++){
            for(j=0;j<Columnas;j++){
                Matriz[i][j]=0;
                GuardaEn(i ,j , A.Elemento(j,i));
            }
        }
    }

    void Suma(cMatriz A, cMatriz B){                //Suma de matrices
            for(i=0;i<Filas;i++){
                for(j=0;j<Columnas;j++){
                    GuardaEn(i ,j , A.Elemento(i,j) + B.Elemento(i,j));
                }
            }
    }

    void Multiplicar(cMatriz A, cMatriz B){                //Multiplicacion de matrices

        for(i=0;i<2;i++){                                   //se puso val de 2, para cambiar rangos asi como activar el condicional de filas=columnas hay que activar en el main las variables
            for(j=0;j<2;j++){
                Matriz[i][j]=0;
                for(k=0;k<2;k++){
                    GuardaEn(i ,j ,( Matriz[i][j] +( A.Elemento(i,k)*B.Elemento(k,j))));
                }

            }
        }

    }

    double Elemento(int Fila, int Columna){
        return Matriz[Fila][Columna];
    }

private:
    int i, j, k;
    int Filas, Columnas;
    double **Matriz;                                // donde **Matriz es igual a Matriz[][]
};


#endif // MATRICES

Thanks in advance.

    
asked by Neko-PH 07.10.2016 в 05:54
source

1 answer

2

What is created with new[] must be released with delete[] . If you use delete you end up with memory leaks. In your case the destructor code is wrong:

//  Constructor  Matriz
Matriz=new double*[Filas];
for(i=0;i<Filas;i++){
  Matriz[i]=new double[Columnas];
  for(j=0;j<Columnas;j++)
    Matriz[i][j]=0.0;
}

//Destructor
for(i=0;i<Filas;i++)
  delete Matriz[i];
delete Matriz;

The correct thing would be the following:

//Destructor
for(i=0;i<Filas;i++)
  delete[] Matriz[i];
delete[] Matriz;

As for what you mention about the error, I recommend you read your question carefully and correct it, as you do not understand the problem.

Also, this question has nothing to do with Qt (it does not matter if the main use Qt or compile from QtCreator if you do not link to any Qt library), so I would recommend removing that tag along with all the references to Qt.

Greetings.

    
answered by 07.10.2016 / 08:57
source