c++ - 类间内存分配重合

标签 c++ visual-studio-2010 visual-c++

我正在计算通过管道的边界层流的特性。我有一个 CChannel 类,它存储管道的几何形状,CFlow 保存流体的全局属性,CNode 保存边界层的局部参数。 当我以当前形式执行程序时,CChannel 内的 GridPoints vector (变量“alpha”)的第一个元素被分配了与 Uinf 相同的内存位置,它是 CFlow 类的私有(private)成员。当我更改后一个类以使其包含的字段不再是指针而是常规变量时,问题就消失了。我还尝试在类构造函数中为 GridPoints vector 保留内存空间,但没有任何效果。当我寻找答案时,我发现这可能是由内置代码优化器引起的,但没有设法学习其他任何东西。 (如果是这样,我怎样才能在不损失效率的情况下解决这个问题?)我猜测问题的出现是由于两种不同的内存分配模式(堆与堆栈)之间的差异。我仍然想知道为什么会发生这种情况,以便我可以将全局流参数存储为指针并避免将来出现此问题。

程序.cpp

#include <iostream>

#include "Channel.h"    // stores the channel geometry
#include "Flow.h"   // stores the fluid properties and free stream data
#include "Node.h"       // holds the local BL flow properties, e.g. BL thickness, lambda, etc.

using namespace std;

int main(void)
{
    int NoNodes=21;
    CChannel MyChan(4, 1.2, .8);    // L, h1, h2
    MyChan.MeshUniform(NoNodes);

    CFlow Flow1(.5,1.529e-5,1.19);  // Uinf, niu, ro

    for (int i=0;i<NoNodes;i++)
    {
        MyChan.GridPoints->at(i).GetAlpha();
    }
    return(0);
}

Node.h

#pragma once
class CNode
{
public:
    double *alpha, *x, *lambda; // properties dependent on the Pollhausen velocity profile

    CNode(void);
    ~CNode(void);

    void GetAlpha(void);    // calculates alpha

};

Node.cpp

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

CNode::CNode(void)
{
    alpha=new double;

    lambda=new double;
    *lambda=0;
}

CNode::~CNode(void)
{
    delete alpha, x, lambda;
}

void CNode::GetAlpha(void)
{
    *alpha=(.3-*lambda/120.);
}

流.h

#pragma once
class CFlow
{
private:
    double *Uinf, *niu, *ro;
public:
    CFlow(double, double, double);
    ~CFlow(void);
};

流.cpp

#include "Flow.h"

CFlow::CFlow(double u, double visc, double den)
{
    Uinf=new double;
    niu=new double;
    ro=new double;
    *Uinf=u;    // free stream velocity (assumes the inflow is parallel to the channel's CL) [m/s]
    *niu=visc;  // kinematic viscosity of the fluid [m^2/s]
    *ro=den;    // density of the fluid [kg/m^3]
}


CFlow::~CFlow(void)
{}

Channel.h

#pragma once
#include <vector>

#include "Node.h"

class CChannel
{
public:
    double *L, *h1, *h2;    // h1 & h2 defined from the CL => make use of the problem assumed to be symmetric
    std::vector<CNode> *GridPoints; // stores data for each individual grid point

    CChannel(double, double, double);
    ~CChannel(void);

    void MeshUniform(int);  // creates a uniform distribution of nodes along the length of the channel
};

Channel.cpp

#include "Channel.h"

CChannel::CChannel(double length,double height1,double height2)
{
    L=new double;   // allocate memory
    h1=new double;
    h2=new double;
    GridPoints = new std::vector<CNode>;

    *L=length;      // assign input values
    *h1=height1;
    *h2=height2;
}


CChannel::~CChannel(void)
{
    delete L, h1, h2, GridPoints; // delete all the members of the class
}


void CChannel::MeshUniform(int NoNodes)
{
    GridPoints->resize(NoNodes);    // resize the vector
    double dx=*L/(NoNodes-1);   // increment of length between each pair of nodes
    for (int i=0; i<NoNodes; i++)
        *GridPoints->at(i).x=0.+i*dx;   // assign the location to each node
}

最佳答案

如前所述,您不需要所有这些指针 - 将它们更改为原始变量。

如果有一天您遇到需要指针的情况,请记住三法则:What is The Rule of Three? .

你没有在你的类中定义复制构造函数和赋值运算符,打破了这条规则,就像在这个特定的类中一样:

class CNode
{
public:
    double *alpha, *lambda; // properties dependent on the Pollhausen velocity profile

    CNode(void);
    ~CNode(void);

    void GetAlpha(void);    // calculates alpha

};

您正在使用这个 CNodestd::vector<CNode> - 所以你正在遭受这个 CNode 的错误复制对象。

因此您需要添加复制构造函数和赋值运算符 - 那么即使您仍将使用指针,您的问题也应该消失,就像在这个简单的示例类中一样:

class Example {
public:
  Example() : p(new int()) {}
  ~Example() { delete p; }
  Example(const Example& e) p(new int(e.p?*e.p:0)) {}
  Example& operator = (Example e)
  {
      std::swap(e.p, p);
      return *this;
  }
private:
  int* p;  
};

关于c++ - 类间内存分配重合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103409/

相关文章:

c++ - 使用隐式转换而不是 QueryInterface() 进行向上转换是否合法且具有多重继承?

c++ - std::bitset 是否保证结构中的连续内存和恒定大小(以避免填充?)

c++ - 在 Ada95/构造函数和受控类型中使用 C++ 类

asp.net - 打开 web.config 文件会导致 Visual Studio 2010 Ultimate 挂起并崩溃

c++ - 谁对谁错又名 GCC vs Visual Studio

visual-studio-2008 - Visual Studio 2010 和 Windows 2000

c++ - 小缓冲区优化和对齐

C++ 中的 Python 或纯 C++ 用于文件模板操作?

visual-studio - 在 VS2010 上构建/使用 CppUnit 库

c# - VS2010 中的 2.0 框架 .net 引用缺少 System.web.extensions 引用 dll?