0
I created a program in c++ to manipulate matrices,and I divided the source code into 2 files, one with functions and the other with implementation, all functions work correctly, but when starting any function for the second time,returns a float Segmentation error, any function started for 1 time works perfectly,I would like a help, thanks in advance.
//main.cpp
#include <iostream>
#include "func.cpp"
using namespace std;
int main()
{
matriz *m1 = criaMatriz(5,5);
atribuiMatriz(2,1,m1,45.4);
//cout << linhasMatriz(m1) << endl;
//cout << *(*((*m1).mat + 1)) << endl;
//cout << m1 << endl;
cout << acessaMatriz(2,1,m1) << endl;
//cout << m1 << endl;
cout << acessaMatriz(2,1,m1) << endl;
//cout << (*m1).lin << endl;
//cout << linhasMatriz(m1) << endl;
//liberaMatriz(m1);
return 0;
}
//func.cpp
Citation
#include <iostream>
using namespace std;
struct matriz
{
int lin;
int col;
double **mat;
}typedef matriz;
matriz* criaMatriz(int lin,int col)
{
matriz *a,b;
a = &b;
(*a).lin = lin;
(*a).col = col;
(*a).mat = new double*[lin];
for(int i = 0;i < lin;i++)
*((*a).mat + i) = new double[col];
//okokokokok
return a;
}
void liberaMatriz(matriz *a)
{
int lin = (*a).lin;
for(int i = 0;i < lin;i++)
delete[] *((*a).mat + i);
delete[] (*a).mat;
}
double acessaMatriz(int lin,int col,matriz *a)
{
return *(*((*a).mat + lin - 1) + col - 1);
}
void atribuiMatriz(int lin,int col,matriz *a,double comp)
{
*(*((*a).mat + lin - 1) + col - 1) = comp;
}
int linhasMatriz(matriz *a)
{
int lin = (*a).lin;
return lin;
}
int colunasMatriz(matriz *a)
{
return (*a).col;
}
Looking so close, include files
cpp
orc
is seriously disheartened. The functioncriaMatriz
is returning a local address which will never work.– Isac
It’s really messed up:
return *(*((*a).mat + lin - 1) + col - 1);
– gfleck