Illegal hardware instruction / Illegal instruction: 4 when running some programs in C ++ from macOS

0

I have been having this problem for a few days and I can not find a solution. When doing programs in C ++, when wanting to run them in macOS (Macbook Air early 2015) I skipped "Illegal instruction: 4 from 'bash' and" Illegal hardware instruction "from 'zsh'

$ ./matrices < datos_m_3.txt
[1]    71856 illegal hardware instruction  ./matrices < datos_m_3.txt

I tried to reinstall the Xcode CLT, the gcc compiler, even formatted the Mac and with factory values still, I still give this error, when I never gave it.

* The fact is that in Windows and Linux (Ubuntu) if I run without this error, even with programs from previous years made with the same Mac, it does not let me execute them.

Thanks in advance.

The code is distributed in three files: a main, a .hpp of headers and a .cpp

main_m_3.cpp

#include "matrices_3.hpp"

int main(void)
{
    matrix_t A;
    matrix_t B;

    cout << endl;
    A.read(cin);
    cout << "---  MATRIZ ORIGINAL  ---" << endl;
    A.write();

    cout << "---  PRECISIÓN 0.0001 ---" << endl;
    A.filtra(B,5.254,1E-4);
    B.write();

    cout << "---  PRECISIÓN 0.001  ---" << endl;
    A.filtra(B,5.254,1E-3);
    B.write();

    cout << "---  PRECISIÓN 0.01   ---" << endl;
    A.filtra(B,5.254,1E-2);
    B.write();

    cout << "---  PRECISIÓN 0.1    ---" << endl;
    A.filtra(B,5.254,1E-1);
    B.write();

    cout << "---  PRECISIÓN 1.0    ---" << endl;
    A.filtra(B,5.254,1.0);
    B.write();

    cout << "---  MATRIZ ORIGINAL  ---" << endl;
    A.write();

    cout << "---  MATRIZ ORIGINAL TRASPUESTA---" << endl;
    A.trasponer();
}

matrices_3.hpp

#pragma once

#include <cstdio>       // Cabecera que contendrá el procedimiento sprintf
#include <iostream>     // Cabecera que tendrá la E/S mediante flujo
#include <cmath>        // Cabecera que contendrá las funcioens matemáticas

using namespace std;


//========================================================================================
// Definición de tipos
//========================================================================================


typedef double      matrix_item_t;  

typedef unsigned short int  matrix_inx_t;   

typedef unsigned short int  vector_inx_t;   

class matrix_t {

//========================================================================================
// Atributos privados.
//========================================================================================

private:

    matrix_item_t*  M_; /**< Puntero que apunta al comienzo del vector conteniendo los elementos de la matriz. */

    matrix_inx_t    m_; /**< Número de filas.   */
    matrix_inx_t    n_; /**< Número de columnas.*/



//========================================================================================
// Métodos públicos.
//========================================================================================

public:

    matrix_t(matrix_inx_t m,matrix_inx_t n); 
    matrix_t(void);     
    ~matrix_t(void);

    matrix_item_t get_matrix_item(matrix_inx_t i,matrix_inx_t j) const;

    void set_matrix_item(matrix_inx_t i,matrix_inx_t j,matrix_item_t it);

    matrix_inx_t get_m(void) const;

    matrix_inx_t get_n(void) const;

    istream& read(istream& is);
    ostream& write(ostream& os) const;

    void write(void) const;

    bool igual(matrix_item_t a, matrix_item_t b, double precision);
    bool mayor(matrix_item_t a, matrix_item_t b, double precision); 
    bool menor(matrix_item_t a, matrix_item_t b, double precision);
    bool cuadrado(matrix_item_t a, matrix_item_t b, double precision);
    bool zero(matrix_item_t a, double precision);
    void filtra(matrix_t& M, matrix_item_t it, double precision);
    void trasponer(void);
    void submatrices(void);

//========================================================================================
// Métodos privados.
//========================================================================================

private:

    vector_inx_t pos(matrix_inx_t i,matrix_inx_t j);
    vector_inx_t pos(matrix_inx_t i,matrix_inx_t j) const;

    void crearMatriz(void); 

    void destruirMatriz(void);

    void redimensiona(matrix_inx_t m,matrix_inx_t n);

};

matrices_3.cpp

    #include "matrices_3.hpp"
#include <iomanip>

using namespace std;


//========================================================================================
// Métodos privados.
//========================================================================================



vector_inx_t matrix_t::pos(matrix_inx_t i,matrix_inx_t j)
{
    if ((i<1)||(i>m_)||(j<1)||(j>n_)){
        cerr << "Error accediendo a matriz"<< endl;
        return 0;
        }

    return (i-1)*n_+j-1;


}

vector_inx_t matrix_t::pos(matrix_inx_t i,matrix_inx_t j) const
{
    if ((i<1)||(i>m_)||(j<1)||(j>n_)){
        cerr << "Error accediendo a matriz"<< endl;
        return 0;
        }

    return (i-1)*n_+j-1;


}


void matrix_t::crearMatriz(void) 
{
    M_= new matrix_item_t [m_*n_];  // Crea un vector de mxn elementos. 

    if (M_==NULL)       // Si ha fallado la reserva de memoria. 
        cerr << "Error creando matriz." << endl;
}       



void matrix_t::destruirMatriz(void)
{
    if (M_!=NULL){
        delete [] M_;       // Libera la memoria previamente reservada para la matriz.
        M_=NULL;        // Asigna NULL al puntero.
    }

    m_=0;
    n_=0;
}




void matrix_t::redimensiona(matrix_inx_t m,matrix_inx_t n)
{
    destruirMatriz();

    m_=m;
    n_=n;

    crearMatriz();
}




//========================================================================================
// Métodos públicos.
//========================================================================================

matrix_t::matrix_t(matrix_inx_t m,matrix_inx_t n):
M_(NULL),
m_(m),
n_(n)
{
    crearMatriz();
}   




matrix_t::matrix_t(void):
M_(NULL),
m_(0),
n_(0)
{}      



matrix_t::~matrix_t(void)
{
    destruirMatriz();
}


matrix_item_t matrix_t::get_matrix_item(matrix_inx_t i,matrix_inx_t j) const
{
    return M_[pos(i,j)];
}


void matrix_t::set_matrix_item(matrix_inx_t i,matrix_inx_t j,matrix_item_t it)
{
    M_[pos(i,j)]=it;    
}




matrix_inx_t matrix_t::get_m(void) const
{
    return m_;
}




matrix_inx_t matrix_t::get_n(void) const
{
    return n_;
}




istream& matrix_t::read(istream& is)
{
    int m,n;

    is >> m >> n;

    redimensiona(m,n);

    const int sz=m*n;

    for(int i=0;i<sz;i++)
        is >> M_[i];

}



ostream& matrix_t::write(ostream& os) const
{

    os << setw(10) << get_m() << setw(10) << get_n() << endl;

    for(int i=1;i <= get_m();i++){

        for(int j=1;j <= get_n() ;j++)
            os << setw(10) << fixed << setprecision(6) << get_matrix_item(i,j);

        os << endl;
    }
}

void matrix_t::write(void) const 
{


    for(int i=1;i <= get_m();i++){
        cout << "|";
        for(int j=1;j <= get_n();j++)
            cout << setw(10) << fixed << setprecision(6) << get_matrix_item(i,j);
        cout << " |";
        cout << endl;
    }

    cout << endl;
}

//-----------------------------------------------------
//FASE 2
//-----------------------------------------------------
bool matrix_t::igual(matrix_item_t a, matrix_item_t b, double precision)
{
  if (fabs((a-b))<precision) {
   return (true);
  }
  else {
   return (false);
  }
}

bool matrix_t::mayor(matrix_item_t a, matrix_item_t b, double precision)
{
  if ((a-b)>precision) {
   return (true);
  }
  else {
   return (false);
  }
}

bool matrix_t::menor(matrix_item_t a, matrix_item_t b, double precision)
{
  if ((a-b)<-(precision)) {
   return (true);
  }
  else {
   return (false);
  }
}

bool matrix_t::zero(matrix_item_t a, double precision)
{
  if (fabs(a)<precision) {
   return (true);
  }
  else {
   return (false);
  }
}

bool matrix_t::cuadrado(matrix_item_t a, matrix_item_t b, double precision)
{

  if (fabs((a*a)-b)<precision){
   return (true);
  }
  else {
   return (false);
  }
}

//-----------------------------------------------------
//Fase 3
//-----------------------------------------------------
void matrix_t::filtra(matrix_t& M, matrix_item_t it, double precision)
{
  M.redimensiona(get_m(), get_n());
    for (int i=1; i<=get_m(); i++)
      for (int j=1; j<=get_n(); j++){
        if (igual(get_matrix_item(i,j),it,precision))
          M.set_matrix_item(i, j, get_matrix_item(i,j));
        else 
          M.set_matrix_item(i, j,0.0); 
        }
}

//-----------------------------------------------------
//Fase 4
void matrix_t::trasponer(void)
{
    for(int j=1; j<=get_n(); j++){
        cout << "|";
        for(int i = 1; i <=get_m(); i++){ 
            cout << setw(10) << fixed << setprecision(6) << get_matrix_item(i,j);
        }
        cout << " |";
        cout << endl;
    }
    cout << endl;
}  

And to this program, a text file is passed to it with the data of the matrix.

datos_m_3.txt

          5           3 

   5.254560    5.254570    5.254580 
   5.354560    5.254000    5.254100 
   5.654560    5.354570    5.255560 
   5.754560    6.000000    5.354580 
   5.254580    5.300000    5.254590

And finally, this is the Makefile that I created and am using.

CXX=g++ 
CXXFLAGS=-g

OBJS = main_m_3.cpp matrices_3.cpp

all: ${OBJS}
    $(CXX) $(CXXFLAGS) -o main ${OBJS}

clear: 
    rm -rf ${OBJS}
    
asked by ozzrocker95 28.02.2018 в 20:09
source

1 answer

0

Test specifying your version of macOS with the flag

  

-mmacosx-version-min = version

The earliest version of MacOS X that this executable will run on is version. Typical values of version include 10.1, 10.2, and 10.3.9.

If the compiler was built to use the system’s headers by default, then the default for this option is the system version on which the
     

compiler is running, otherwise the default is to make choices that are   compatible with as many systems and code bases as possible.

I leave the link because there are more options GCC | 3.18.10 Darwin Options

    
answered by 05.03.2018 в 14:18