从来没有见过这个Seg错误,帮助你可以帮助我。在 GDB 中,我得到了这个:
Program received signal SIGSEGV, Segmentation fault.
#0 0x0000000000401c08 in InvLib::pinv_jazz (
this=<error reading variable: Cannot access memory at address 0x7fffff766188>,
r=<error reading variable: Cannot access memory at address 0x7fffff766184>,
co=<error reading variable: Cannot access memory at address 0x7fffff766180>,
A=<error reading variable: Cannot access memory at address 0x7fffff766178>,
Ai=<error reading variable: Cannot access memory at address 0x7fffff766170>)
at ../eigen/../inc/InvLib.h:288
288 void pinv_jazz(const int r, const int co, double *A, double *Ai) {
#1 0x00000000004022de in init_Ai (this=<optimized out>) at ../eigen/../inc/InvLib.h:624
#2 InvLib::test_col_updating () at ../eigen/../inc/InvLib.h:918
#3 0x000000000040fb2a in InvLib::test_lib () at ../eigen/../inc/InvLib.h:1140
#4 0x0000000000400c88 in main (argc=1, argv=0x7fffffffe758) at til.cc:6
据我了解, InvLib::pinv_jazz 采用的所有值都是无效的,这很奇怪。请让我先提供 init_Ai 和 pinv_jazz 的定义:
class InvLib {
...
// initialize Ai to be pinv(A) using SVD
inline __attribute__((always_inline))
void init_Ai() {
printf ("%p\n", this);
printf ("%d %d\n", m_r, m_co);
printf ("%p %p\n", m_A, m_Ai);
pinv_jazz(m_r, m_co, m_A, m_Ai);
}
// compute SVD-inv on a mat_t matrix
void pinv_jazz(const int r, const int co, double *A, double *Ai) {
static CoeffMat eA, eAi;
static int i, j;
static double *A_, *Ai_;
assert(r <= MAXSIZE);
assert(co <= MAXSIZE);
printf ("my %p %p\n", A, Ai);
eA.resize(r, co);
A_ = A;
for (i = 0; i < r; i++) {
for (j = 0; j < co; j++) {
eA(i, j) = Complex(*A_, *(A_+1));
A_ += 2;
}
}
//eAi = eigen_pinv(&eA);
{
Eigen::JacobiSVD<CoeffMat> svdx(eA, Eigen::ComputeThinU
| Eigen::ComputeThinV);
Eigen::MatrixXf Ss;
int n = svdx.singularValues().asDiagonal().rows();
printf ("%d\n", n);
Ss.resize(n, 1);
int i;
for (i = 0; i < n; i++) {
Ss(i) = svdx.singularValues()(i);
}
CoeffMat S;
S.resize(n, 1);
for (i = 0; i < n; ++i) {
if ( Ss(i) > TOLE_PINV )
{ S(i)=1.0/Ss(i); }
else
{ S(i)=0; }
}
eAi = svdx.matrixV() * S.asDiagonal() * svdx.matrixU().adjoint();
}
}
} //InvLib
首先我不明白为什么 pinv_jazz 在 GDB 的输出中写为 InvLib::pinv_jazz 而 init_Ai 写为 init_Ai?它们都是非静态函数!
其次,GDB 抱怨它无法访问 0x7fffff766188 等处的内存,但是在 init_Ai 中,在调用 pinv_jazz 之前,我打印了 m_A 和 m_Ai 的值以放入 pinv_jazz,它们不是 GDB 抱怨的 0x7fffff766178 或 0x7fffff766170。我想知道GDB从哪里得到地址0x7fffff766178和0x7fffff766170?
第三个奇怪的是,如果我删除了 eAi = svdx.matrixV() * S.asDiagonal() * svdx.matrixU().adjoint(); 然后它运行顺利,没有错误。但我不明白为什么这条线会导致上述错误。
该代码是使用带有 -O0 的 GCC 4.6.3 编译的。请帮我!!