Problem with calling lapack_make_complex_{float,double} with C/C++

I wrote a simple test routine:

for(int i = 0; i < 100;++i)
 {
  std::cout<< "i="<< i <<": ";
  print_complex(lapack_make_complex_double(1,0));
  std::cout<< std::endl;
 }
where print_complex prints the complex to console.

The routine will end with an exception:



First-chance exception at 0x00420fd0 in LTELinkLevelSimulator.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x770b15de in LTELinkLevelSimulator.exe:0xC0000005: Access violation reading location 0x00000000.

 and the printed complexes on the console are:


See, something goes wrong. The complex should be 1+0j, but the lapack_make_complex_double function, which is a C interface to create a complex with double precision, is not stable.

I have to write my own make_complex routine instead. It looks like:

lapack_complex_double make_complex(constdouble a, const double b)
{
 lapack_complex_double c;
 c.real = a;
 c.imag = b;

 return c;
}
If replace the lapack_make_complex_double with make_complex, the output is:


No error happens again!

One last word, when using the CBLAS and LAPACKE library in your C/C++ code, be very careful of such flaws.

猜你喜欢

转载自blog.csdn.net/jeffyko/article/details/79333404