How to fix munmap_chunk(): invalid pointer when the program returns from the main in C++ with std::vector

huangapple go评论66阅读模式
英文:

How to fix munmap_chunk(): invalid pointer when the program returns from the main in C++ with std::vector

问题

我在主函数返回时遇到错误 munmap_chunk(): invalid pointer。我已经运行了调试器并注意到,当程序从 main 返回并跳转到文件 new_allocator.h::operator delete(__p, ... 行时,错误就会发生。

#include <iostream>
#include <vector>
#include <cmath>

// 解决矩阵方程 Ax = b。
// A 是大小为 n x n 的方阵。
// x 是大小为 n 的向量。
// b 是大小为 n 的向量。
std::vector<double> solve(std::vector<std::vector<double>>& A, std::vector<double>& b) {
    // 函数实现
}

int main() {
    // 主函数实现
}

程序输出:
-2.28571
-2.28571
9.71429
10
10
9.71429
-1.14286
-0.0714286
-1.14286
-0.0714286
1.21429
munmap_chunk(): invalid pointer

我查看了 Stack Overflow 上类似的问题,但他们都是使用 `malloc`  `new` 来分配内存,而我不是。我没有分配任何动态内存,所有我使用的内存都在堆栈中!我知道类 `std::vector` 分配内存,但我认为它由类自身管理,我不应该担心它。

我尝试了以下方法来解决问题:
- 在调试器中运行程序
- 检查 Stack Overflow 上类似的问题
- 添加 `A.clear(); x.clear(); b.clear();`

这些解决方案都没有奏效。导致这个问题的原因是什么?我该如何解决?

<details>
<summary>英文:</summary>

I am getting the error `munmap_chunk(): invalid pointer` when the program returns from the main function. I have run the debugger and I noticed that the error happens when the program return from `main` and jumps to the file `new_allocator.h` at the line `::operator delete(__p, ...`

Code:

    #include &lt;iostream&gt;
    #include &lt;vector&gt;
    #include &lt;cmath&gt;
    
    // Solves the matrix equation Ax = b.
    // A is a square matrix of size n x n.
    // x is a vector of size n.
    // b is a vector of size n.
    std::vector&lt;double&gt; solve(std::vector&lt;std::vector&lt;double&gt;&gt;&amp; A, std::vector&lt;double&gt;&amp; b) {
        int n = A.size();
        for (int k = 0; k &lt; n; k++) {
            int iMax = k;
            double maxVal = std::abs(A[k][k]);
            for (int i = k+1; i &lt; n; i++) {
                if (std::abs(A[i][k]) &gt; maxVal) {
                    iMax = i;
                    maxVal = std::abs(A[i][k]);
                }
            }
            if (maxVal &lt; 1e-10) {
                throw std::runtime_error(&quot;A is not invertible&quot;);
            }
            std::swap(A[k], A[iMax]);
            std::swap(b[k], b[iMax]);
            for (int i = k+1; i &lt; n; i++) {
                double factor = A[i][k] / A[k][k];
                for (int j = k+1; j &lt; n+1; j++) {
                    A[i][j] -= factor * A[k][j];
                }
                A[i][k] = 0;
                b[i] -= factor * b[k];
            }
        }
        std::vector&lt;double&gt; x(n);
        for (int i = n-1; i &gt;= 0; i--) {
            double sum = 0;
            for (int j = i+1; j &lt; n; j++) {
                sum += A[i][j] * x[j];
            }
            x[i] = (b[i] - sum) / A[i][i];
        }
        return x;
    }
    
    
    
    int main() {
        std::vector&lt;std::vector&lt;double&gt;&gt; A = {
    		{0.5,   0,     0,     0,     0,     0,      0,    0,    -1,   0,    0},       
    		{0,     0,     0,     0,     0,     0,      -1,   0,    1,    0,    0},       
    		{0,     0,     0.25,  -0.25, 0,     0,      1,    0,    0,    0,    1},       
    		{0,     0,     -0.25, 0.25,  0,     0,      0,    0,    0,    1,    0},       
    		{0,     0,     0,     0,     0,     0,      0,    1,    0,    -1,   0},       
    		{0,     0,     0,     0,     0,     0.125,  0,    0,    0,    0,    -1},      
    		{0,     -1,    1,     0,     0,     0,      0,    0,    0,    0,    0},       
    		{0,     0,     0,     0,     1,     0,      0,    0,    0,    0,    0},       
    		{-1,    1,     0,     0,     0,     0,      0,    0,    0,    0,    0},       
    		{0,     0,     0,     1,     -1,    0,      0,    0,    0,    0,    0},       
    		{0,     0,     1,     0,     0,     -1,     0,    0,    0,    0,    0}
    	};
    		
        std::vector&lt;double&gt; b = {0, 0, 0, 0, 0, 0, 12, 10, 0, 0, 0};
        
        // Solve the equation Ax = b.
    	auto x = solve(A, b);
    
    	// Print the solution.
    	for (int i = 0; i &lt; x.size(); i++) {
    	  std::cout &lt;&lt; x[i] &lt;&lt; std::endl;
    	}
        return 0;
    }

The output of the program:

    -2.28571
    -2.28571
    9.71429
    10
    10
    9.71429
    -1.14286
    -0.0714286
    -1.14286
    -0.0714286
    1.21429
    munmap_chunk(): invalid pointer


I have checked similar questions on Stack Overflow but all of them allocate memory using `malloc` or `new` which is not my case. I don&#39;t allocate any dynamic memory, all the memory I use is in the stack! I know the class `std::vector` allocate memory but I think it is managed by the class itself and I shouldn&#39;t worry about it.

I have tried the following to fix the problem:
- Running the program in a debugger
- Checking for similar questions on Stack Overflow
- Adding `A.clear(); x.clear(); b.clear();`

None of these solutions have worked.

What is causing this issue? How can I solve it?

</details>


# 答案1
**得分**: 1

```none
如果你在[Valgrind](https://valgrind.org/)下运行你的程序,你会立即看到问题所在:

```none
...
==2018== Invalid read of size 8
==2018==    at 0x109635: solve(...) (1.cpp:28)
==2018==    by 0x10A479: main (1.cpp:65)
==2018==  Address 0x4db75a8 is 0 bytes after a block of size 88 alloc'd
...
==2018== Invalid write of size 8
==2018==    at 0x10963E: solve(...) (1.cpp:28)
==2018==    by 0x10A479: main (1.cpp:65)
==2018==  Address 0x4db75a8 is 0 bytes after a block of size 88 alloc'd
...

第28行是:

A[i][j] -= factor * A[k][j];

查看for循环的终止条件,j < n + 1,问题就显而易见了。


<details>
<summary>英文:</summary>
If you run your program under [Valgrind](https://valgrind.org/), you&#39;ll instantly see where the problem is:
```none
...
==2018== Invalid read of size 8
==2018==    at 0x109635: solve(...) (1.cpp:28)
==2018==    by 0x10A479: main (1.cpp:65)
==2018==  Address 0x4db75a8 is 0 bytes after a block of size 88 alloc&#39;d
...
==2018== Invalid write of size 8
==2018==    at 0x10963E: solve(...) (1.cpp:28)
==2018==    by 0x10A479: main (1.cpp:65)
==2018==  Address 0x4db75a8 is 0 bytes after a block of size 88 alloc&#39;d
...

Line 28 is:

A[i][j] -= factor * A[k][j];

Looking into the for loop termination condition, j &lt; n + 1, the problem becomes obvious.

huangapple
  • 本文由 发表于 2023年5月14日 20:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定