英文:
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 <iostream>
#include <vector>
#include <cmath>
// 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<double> solve(std::vector<std::vector<double>>& A, std::vector<double>& b) {
int n = A.size();
for (int k = 0; k < n; k++) {
int iMax = k;
double maxVal = std::abs(A[k][k]);
for (int i = k+1; i < n; i++) {
if (std::abs(A[i][k]) > maxVal) {
iMax = i;
maxVal = std::abs(A[i][k]);
}
}
if (maxVal < 1e-10) {
throw std::runtime_error("A is not invertible");
}
std::swap(A[k], A[iMax]);
std::swap(b[k], b[iMax]);
for (int i = k+1; i < n; i++) {
double factor = A[i][k] / A[k][k];
for (int j = k+1; j < n+1; j++) {
A[i][j] -= factor * A[k][j];
}
A[i][k] = 0;
b[i] -= factor * b[k];
}
}
std::vector<double> x(n);
for (int i = n-1; i >= 0; i--) {
double sum = 0;
for (int j = i+1; j < n; j++) {
sum += A[i][j] * x[j];
}
x[i] = (b[i] - sum) / A[i][i];
}
return x;
}
int main() {
std::vector<std::vector<double>> 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<double> 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 < x.size(); i++) {
std::cout << x[i] << 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'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'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'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'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
...
Line 28 is:
A[i][j] -= factor * A[k][j];
Looking into the for
loop termination condition, j < n + 1
, the problem becomes obvious.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论