gpt4 book ai didi

c++ - 循环内奇怪的内存泄漏

转载 作者:行者123 更新时间:2023-11-28 01:29:35 24 4
gpt4 key购买 nike

我的工作项目有内存泄漏问题。实际上,我已经阅读了一些关于内存泄漏的引用资料,发现每个 new 都应该是 delete 然后,但不幸的是,我无法在循环时删除指针尚未结束。那时我需要建议来处理这个问题。

主程序:

#include <iostream>
#include <iomanip>
#include "linspace.h"
#include "matrik.h"
#include "vektor.h"
#include "FV.h"

using namespace std;
matrik WENO5CPLFRK3(matrik w, double aumax, double dx, double dt, int Ne, int Nt);

int main()
{
double CFL, tm, t;
CFL = 0.5;
tm = 1;
t = .0;


int Ne, Np;
Ne = 1000;
Np = Ne + 1;

int Ng, Nt;
Ng = 3;
Nt = Ne + Ng + Ng;

double lb, rb, dx;
vektor x(Np);
matrik w(3, Ne);
matrik q(3, Ne), qn(3, Ne), qm(3, Ne);
matrik dF(3, Ne);
double aumax, dt;
lb = 0.;
rb = 1.;
dx = (rb - lb)/Ne;

x = linspace(lb, rb, Np);
w = initial(x, Ne);
aumax = soundspeedmax(w, Ne);
dt = CFL*dx/aumax;

int iter = 0;

while (t < tm){
cout << "Iteration: "<< ++iter << endl;
w = WENO5CPLFRK3(w,aumax,dx,dt,Ne,Nt);
t += dt;
aumax = soundspeedmax(w, Ne);
dt = CFL*dx/aumax;
// cout << t<< endl;
}

for (int i = 0; i < Ne; i++){
cout <<setprecision(10) << w[0][i] << "," << setprecision(3) << w[1][i] << "," << setprecision(3) << w[2][i] << endl;
}

return 0;
}

matrik WENO5CPLFRK3(matrik w, double aumax, double dx, double dt, int Ne, int Nt)
{
matrik dF(3, Ne), qn(3, Ne), q(3, Ne);

qn = Q(w, Ne);

dF = WENO5LFCMP(w, aumax, dx, Ne, Nt);
//
q = qn - dt*dF;
w = W(q, Ne);
dF = WENO5LFCMP(w, aumax, dx, Ne, Nt);

q = 0.75*qn + 0.25*(q - dt*dF);
w = W(q, Ne);
dF = WENO5LFCMP(w, aumax, dx, Ne, Nt);

q = (qn + 2.*(q - dt*dF))/3.;
w = W(q, Ne);
return w;
}

我发现 dF 是内存泄漏的原因。这是我的 WENO5LFCMP 函数:

matrik WENO5LFCMP(matrik w, double aumax, double dx, int Ne, int Nt)
{
int r = 3;
int Ng = (Nt - Ne)/2;
matrik wb(3, Nt);
matrik q(3, Nt);

wb = boundary(w, Ne, Ng);
q = Q(wb, Nt);

double ql[Nt][3], qr[Nt][3];

for (int i = r - 1; i < Nt - r + 1; i++){
for (int k = 0; k < 3; k++){
for (int j = - 2; j < 3; j++){
v[j + 2] = q[k][i - j];
}
vc = WENO5(v);
ql[i+1][k] = vc[1];
qr[i][k] = vc[0];
}
}
delete[] vc;

double** Flux = new double*[Ne + 1];
for(int j = 0; j < Ne + 1; j++){
Flux[j] = new double[3];
}

for (int i = r; i < Nt-r+1; i++){
Flux[i -r] = LFFlux(ql[i], qr[i], aumax);
}

matrik dF(3, Ne);

for (int i = 0; i < Ne; i++){
for (int j = 0; j < 3; j++){
dF[j][i] = (Flux[i + 1][j] - Flux[i][j])/dx;
}
}

for(int j = 0; j < Ne + 1; j++){
delete[] Flux[j];
} delete[] Flux;

return dF;

}

编辑:matrik header :

#ifndef MATRIK_H_INCLUDED
#define MATRIK_H_INCLUDED

class matrik
{
private:
int rows, columns;
double** src;

public:
matrik();
matrik(int ROWS, int COLS);
matrik(const matrik&);
~matrik();

double* operator[](int i) const { return src[i]; }
double& operator()(int i, int j) { return src[i][j]; }

matrik& operator=(const matrik&);
matrik& operator+=(const matrik&);
matrik& operator-=(const matrik&);

friend matrik operator+(const matrik&, const matrik&);
friend matrik operator-(const matrik&, const matrik&);
friend matrik operator*(double, const matrik&);
friend matrik operator*(const matrik&, double);
friend matrik operator/(double, const matrik&);
friend matrik operator/(const matrik&, double);

};

#endif // OVERLOADING_H_INCLUDED

实现:

#include <iostream>
#include "matrik.h"

using namespace std;

matrik::matrik()
{
src = 0;
columns = rows = 0;
}

matrik::matrik(int m, int n)
{
rows = m;
columns = n;

src = new double*[rows];
for (int i = 0; i < rows; i++){
src[i] = new double[columns];
}
}

matrik::matrik(const matrik& M)
{
rows = M.rows;
columns = M.columns;

src = new double*[rows];

for (int i = 0; i < rows; i++){
src[i] = new double[columns];
for (int j = 0; j < columns; j++) src[i][j] = M[i][j];
}
}

inline matrik::~matrik()
{
for(int i = 0; i < rows; i++) delete[] src[i];
delete[] src;
}

matrik& matrik::operator=(const matrik& M)
{
if (this != &M){
if (rows != M.rows || columns != M.columns){
//
}
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
src[i][j] = M[i][j];
}
}
}
return *this;
}

最佳答案

代码与惯用的 C++ 相去甚远。在惯用的 C++ 中 newdelete没有在像你这样的代码中使用。而不是 new[]delete[]你应该使用 std::vector ,这将消除内存泄漏。而不是 new Tdelete你应该使用 std::make_unique<T>() .

话虽如此,即使您保留 newdelete ,代码有几个问题。例如,看一下循环:

for (int i = r - 1; i < Nt - r + 1; i++){
for (int k = 0; k < 3; k++){
for (int j = - 2; j < 3; j++){
v[j + 2] = q[k][i - j];
}
vc = WENO5(v); // <--- allocation
ql[i+1][k] = vc[1];
qr[i][k] = vc[0];
}
}
delete[] vc;

delete[] vc , 最后,表示内存是在较早的分配中分配的:

        vc = WENO5(v);

但是由于分配是在循环中,那么delete[] vc也应该在循环中。否则 future 的迭代将失去以前的值 vc这会泄漏内存。

我希望matrik::operator=中缺失的代码片段如果大小不匹配,则重新分配内存,并且不会泄漏。


我怎么强调都不过分——如果你能避免,就不要使用动态内存分配。留给写 std::vector 的专业人士吧至少 std::make_unique() .

关于c++ - 循环内奇怪的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52199142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com