Search code examples
c++segmentation-faultstack-smash

*** stack smashing detected *** error at the return of a function using FEAST


I have a long program in which I have a function for calculating eigenvalues of a large matrix using FEAST. Right at the return of that function I get a * stack smashing detected * error and I lose all the results. Here is my the function

    void Model::eigensolver(double* val, int* ia, int* ja, const int n, int m0, std::string outfilename)
{
// compute Eigenvalues and Eigenvectors by calling FEAST library from MKL
const char uplo='U';
MKL_INT fpm[128], loop;
feastinit(fpm);
//fpm[0]=1;
const double emin=-1,emax=100;
MKL_INT eig_found=0;
double res,epsout;
double *eigenvalues= new double [m0];
double *eig_vec = new double [m0*_dof];
int info;
std::cout << "Everything ready, making call to FEAST." << std::endl;
dfeast_scsrev(&uplo, &n, val, ia, ja, fpm, &epsout, &loop, &emin, &emax, &m0, eigenvalues, eig_vec, &eig_found, &res, &info );
if (info != 0) {
  std::cout << "Something is wrong in eigensolver. Info=" << info << std::endl;
  exit(0);
}

std::cout << loop << " iterations taken to converge." << std::endl;
std::cout << eig_found << " eigenvalues found in the interval." << std::endl;
std::ofstream evals;
evals.open("evals.dat");
std::cout<<"The eigenfrequencies are:"<<std::endl;
for (int i = 0; i < eig_found; i++) 
  evals << eigenvalues[i] << std::endl;
evals.close();
delete[] eigenvalues;
delete[] eig_vec;
std::cout << "Writen eigenvalues to file evals.dat." << std::endl;
return;
}

The dfeast_scsrev is a function from FEAST library (also part of intel MKL). The error happens right at the return (i.e. after the "Written eigenvalues to file evals.dat." is printing). Depending on the problem I run, sometimes I also get segmentation fault right at the same point.

If I remove the FEAST function call, there is no error. So, I am confused what I am doing wrong. I am trying valgrind, but because of the size of my code, it is taking a long time to run.


Solution

  • Looking at the docs at https://software.intel.com/en-us/node/521749, I see that res should point to an "Array of length m0". Your res is only a single double. Of course, dfeast_scsrev does not know this and writes happily beyond the boundary, thus ruining your stack.

    So the fix is:

    double *res = new double [m0]; instead of double res;