• Jueves 28 de Marzo de 2024, 23:13

Autor Tema:  Clase matriz "violación de segmento"  (Leído 2408 veces)

antlcn

  • Nuevo Miembro
  • *
  • Mensajes: 12
    • Ver Perfil
Clase matriz "violación de segmento"
« en: Viernes 28 de Enero de 2011, 12:37 »
0
Hola, estoy haciendo una clase matriz con sus constructores, operadores, etc y me da un fallo de segmentación tras la comprobación "Ok4" del main, les adjunto el codigo:


main.cpp:
Código: C++
  1. #include <matrix.h>
  2. #include <iostream>
  3. using namespace std;
  4. using namespace storage;
  5.  
  6. //template Function that prints a matrix
  7.  
  8. template<class Type>
  9. void print(Matriz<Type> &M)
  10. {
  11. for(unsigned int i=0;i<M.getNRows();i++){
  12. for(unsigned int j=0;j<M.getNCols();j++)
  13. cout<<M.get(i,j)<< " ";
  14. cout<<endl;
  15. }
  16. }
  17.  
  18. int main(int argc,char **argv)
  19. {
  20. Matriz<float> M;
  21. M.resize(10,10);
  22. cout<<"ok1"<<endl;
  23. M.setIdentity();
  24. cout<<"ok2"<<endl;
  25. print(M);
  26. cout<<"ok3"<<endl;
  27. Matriz<float> M2(M);
  28. print(M2);
  29. cout<<"ok4"<<endl;
  30. Matriz<float> M3(10,10);
  31. M3=M+M2;
  32. cout<<"ok5"<<endl;
  33. print(M3);
  34. cout<<"ok6"<<endl;
  35. //check the result
  36. for(unsigned int i=0;i<M3.getNRows();i++){
  37. for(unsigned int j=0;j<M.getNCols();j++)
  38. if (i!=j)
  39. assert(M3.get(i,j)==0);
  40. else assert(M3.get(i,j)==2);
  41. }
  42. cout<<"Perfect"<<endl;
  43. }
  44.  
  45.  



matrix.h:
Código: C++
  1. #ifndef _Matrix_H_
  2. #define _Matrix_H_
  3. #include <cassert>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7. namespace storage{
  8.     template<class Type>
  9.         class Matriz
  10.         {
  11.             public:
  12.                 /* constructor vacio
  13.                  */
  14.                 inline Matriz();
  15.                 /* constructor copia
  16.                  */
  17.                 inline Matriz(const Matriz &M);
  18.                 /* constructor parametrizado
  19.                  */
  20.                 inline Matriz(unsigned int nRows, unsigned int nCols);
  21.                 /* redimensionar la matriz
  22.                  */
  23.                 inline void resize(unsigned int nRows, unsigned int nCols);
  24.                 /* indica el numero de filas
  25.                 */
  26.                 unsigned int getNRows() { return _nRows;}
  27.                 /* indica el numero de columnas
  28.                  */
  29.                 unsigned int getNCols() { return _nCols;}
  30.                 /* retorna el valor indicado por las variables
  31.                  */
  32.                 inline Type & get(unsigned int r,unsigned int c);
  33.                 /* hace que la matriz sea la indentidad
  34.                  */
  35.                 inline void setIdentity();
  36.                 /* operador de asignacion
  37.                  */
  38.                 inline Matriz & operator=(const Matriz &M);
  39.                 /* realiza la suma de dos matrices
  40.                  */
  41.                 inline Matriz & operator+(const Matriz &M);
  42.                 /* destructor
  43.                  */
  44.                 inline ~Matriz();
  45.                
  46.         private:
  47.             Type **_data;
  48.             unsigned int _nRows;
  49.             unsigned int _nCols;
  50.         };
  51.  
  52.        
  53.  
  54. //////////////////////////
  55. //
  56. //////////////////////////
  57. template<class Type>
  58. Matriz<Type>::Matriz()
  59. {
  60.   _data=NULL;
  61.   _nRows=0;
  62.   _nCols=0;
  63. }
  64.  
  65. //////////////////////////
  66. //
  67. //////////////////////////
  68. template<class Type>
  69. Matriz<Type>::Matriz(const Matriz &M)
  70. {
  71.   int i,j;
  72.   _nRows=M._nRows;
  73.   _nCols=M._nCols;
  74.   _data= new Type*[_nRows];
  75.   for(i=0;i<_nRows;i++){
  76.      _data[i]=new Type[_nCols];
  77.   }
  78.   for(i=0;i<_nRows;i++){
  79.     for(j=0;j<_nCols;j++){
  80.       _data[i][j]=M._data[i][j];
  81.     }
  82.   }
  83. }
  84.  
  85. //////////////////////////
  86. //
  87. //////////////////////////
  88. template<class Type>
  89. Matriz<Type>::Matriz(unsigned int nRows, unsigned int nCols)
  90. {
  91.   int i;
  92.   _nRows=nRows;
  93.   _nCols=nCols;
  94.   if(_data==NULL){
  95.   _data= new Type*[_nRows];
  96.   for(i=0;i<_nRows;i++){
  97.      _data[i]=new Type[_nCols];
  98.   }
  99.   }
  100. }
  101.  
  102. //////////////////////////
  103. //
  104. //////////////////////////
  105. template<class Type>
  106. void Matriz<Type>::resize(unsigned int nRows, unsigned int nCols)
  107. {
  108.   int i;
  109.   if(_data!=NULL){
  110.     for(i=0;i<nRows;i++){
  111.       delete []_data[i];
  112.     }
  113.     delete []_data;
  114.   }
  115.   Matriz(nRows, nCols);
  116. }
  117.  
  118. //////////////////////////
  119. //
  120. //////////////////////////
  121. template<class Type>
  122. Type & Matriz<Type>::get(unsigned int r, unsigned int c)
  123. {
  124.   return _data[r][c];
  125. }
  126.  
  127. //////////////////////////
  128. //
  129. //////////////////////////
  130. template<class Type>
  131. void Matriz<Type>::setIdentity()
  132. {
  133.   int i,j;
  134.   for(i=0;i<_nRows;i++){
  135.     for(j=0;j<_nCols;j++){
  136.       if(i==j)
  137.     _data[i][j]=1;
  138.       else
  139.     _data[i][j]=0;
  140.     }
  141.   }
  142. }
  143.  
  144. //////////////////////////
  145. //
  146. //////////////////////////
  147. template<class Type>
  148. Matriz<Type> & Matriz<Type>::operator=(const Matriz &M)
  149. {
  150.   int i,j;
  151.   if(_data==NULL)
  152.     Matriz(M._nRows, M._nCols);
  153.   for(i=0;i<_nRows;i++){
  154.     for(j=0;j<_nCols;j++){
  155.       _data[i][j]=M._data[i][j];
  156.     }
  157.   }
  158.   return *this;
  159. }
  160.  
  161. //////////////////////////
  162. //
  163. //////////////////////////
  164. template<class Type>
  165. Matriz<Type> & Matriz<Type>::operator+(const Matriz &M)
  166. {
  167.   int i,j;
  168.   if(_data==NULL)
  169.     Matriz(M._nRows, M._nCols);
  170.   for(i=0;i<_nRows;i++){
  171.     for(j=0;j<_nCols;j++){
  172.       _data[i][j]=_data[i][j]+M._data[i][j];
  173.     }
  174.   }
  175.   return *this;
  176. }
  177.  
  178. //////////////////////////
  179. //
  180. //////////////////////////
  181. template<class Type>
  182. Matriz<Type>::~Matriz()
  183. {
  184.   int i;
  185.   if(_data!=NULL){
  186.     for(i=0;i<_nRows;i++){
  187.       delete []_data[i];
  188.     }
  189.     delete []_data;
  190.   }
  191. }
  192. };
  193.  
  194. #endif
  195.  
  196.  


gracias de antemano.

Amilius

  • Miembro HIPER activo
  • ****
  • Mensajes: 665
    • Ver Perfil
Re: Clase matriz "violación de segmento"
« Respuesta #1 en: Viernes 28 de Enero de 2011, 23:32 »
0
El problema está en el uso del constructor en tu operador '=', '+' y el método resize.

Tienes que modificar tu código para mover el código del constructor a resize. De tal forma que sólo tengas new y delete en la función resize. El destructor podría llamar a resize (0,0) para liberar la memoria. Si resize recibe un parámetro que sea 0 debería liberar la memoria y poner _data en 0.

antlcn

  • Nuevo Miembro
  • *
  • Mensajes: 12
    • Ver Perfil
Re: Clase matriz "violación de segmento"
« Respuesta #2 en: Sábado 29 de Enero de 2011, 11:20 »
0
que tendría que hacer en los operadores "+" "=" ? que no veo el fallo.

Amilius

  • Miembro HIPER activo
  • ****
  • Mensajes: 665
    • Ver Perfil
Re: Clase matriz "violación de segmento"
« Respuesta #3 en: Lunes 31 de Enero de 2011, 20:38 »
0
El problema es que no estás reservando memoria y sigues con tu puntero en 0. Esa forma de llamar a un constructor hace que OTRO objeto sea creado en pila y destruido antes de ejecutar la siguiente línea.