有没有办法为容器保留内存块?

发布于 2024-12-04 13:38:00 字数 974 浏览 1 评论 0原文

我希望我能说清楚......

在我的代码中,我定义了一个 box ,其中包含一组元素(bead):

vector<vector<vector<set<std::tr1::shared_ptr<bead> > > > > boxes;

我将元素添加到 < code>box 使用:

boxes[i][j][k].insert(aBead);

由于某种原因,我在这里遇到分段错误。据我所知,分段错误不会因为非法的 beadijk 而发生>,都小于盒子的尺寸并且不是负数。

如果您想知道珠子是:

class particle{
  public:
    vec pos;
    vec oldPos;
    vec vel;
    vec F;
    vec oldF;
    int charge;
    int type;
    double U;
    double nextU;
};

class bead: public particle{
  public: //most of this is redundant...
    int charge;
    int type;
    double rho;
    double nextRho;
    int LID;
    bool keep;
    bool touch;
    double radius;
}

class vec{
  public:
    double x;
    double y;
    double z;
    velarray<double> coor; //on it's way to being canceled
}

I hope I'll be clear....

In my code, I define a box which holds a set of elements (bead):

vector<vector<vector<set<std::tr1::shared_ptr<bead> > > > > boxes;

I am adding element to the box using:

boxes[i][j][k].insert(aBead);

For some reason I get a segmentation fault here. As far as I can tell, The segmentation fault does not occur because of illegal bead and i, j, k, are all smaller than the box's size and are not negative.

In case you wonder bead is:

class particle{
  public:
    vec pos;
    vec oldPos;
    vec vel;
    vec F;
    vec oldF;
    int charge;
    int type;
    double U;
    double nextU;
};

class bead: public particle{
  public: //most of this is redundant...
    int charge;
    int type;
    double rho;
    double nextRho;
    int LID;
    bool keep;
    bool touch;
    double radius;
}

class vec{
  public:
    double x;
    double y;
    double z;
    velarray<double> coor; //on it's way to being canceled
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚我心虐我身 2024-12-11 13:38:00

创建 shared_ptr 时,您需要使用指向使用 new 关键字创建的对象类型的指针来初始化它,而不是使用实际的对象或对象引用。例如:

shared_ptr<bead> ptr(new bead);

不是

bead aBead;
shared_ptr<bead>(aBead);

顺便说一句,也不要执行以下操作:

bead* ptr = new bead();
shared_ptr<bead> sptr(ptr);
delete ptr;

shared_ptr 对象管理指针的生命周期。它将对指针进行引用计数,并在不再有对该指针的引用时在 shared_ptr 析构函数中对指针内部调用 delete。如果您在初始化 shared_ptr 对象后手动调用指针上的 delete ,您最终也会遇到分段错误,因为您基本上已经尝试自行-管理内存生命周期,击败诸如shared_ptr之类的托管“智能”指针的整个指针。

When creating a shared_ptr<T>, you will want to intiailize it with a pointer to the object type that has been created using the new keyword, not with an actual object or object reference. For instance:

shared_ptr<bead> ptr(new bead);

not

bead aBead;
shared_ptr<bead>(aBead);

BTW, also do not do the following:

bead* ptr = new bead();
shared_ptr<bead> sptr(ptr);
delete ptr;

The shared_ptr<bead> object manages the lifetime of the pointer. It will reference count the pointer, and call delete on the pointer internally in the shared_ptr<T> destructor when there are no more references to the pointer. If you manually call delete on the pointer after initializing the shared_ptr<T> object, you will end up with a segmentation fault as well, since you've basically tried to self-manage the memory life-time, defeating the whole pointer of a managed "smart" pointer like shared_ptr<T>.

假装爱人 2024-12-11 13:38:00

如果您支持 C++11,那么创建动态对象加共享指针的首选方法是 make_shared:

boxes[i][j][k].insert(std::make_shared<Bead>());

如果做不到这一点,您将必须自己执行动态分配,但此后的生命周期该对象将由共享指针管理:

boxes[i][j][k].insert(new Bead);

不过,您可能希望更改数据结构,以避免过度使用容器。每个容器都会产生分配,这可能会很昂贵。如果您需要密集地填充 3D 空间,那么您可以使用可大步访问的展平 1D 视图。如果您只需要稀疏点,则可以选择以三元组为键的地图。

If you have support for C++11, then the preferred way to make a dynamic object plus shared pointer is make_shared:

boxes[i][j][k].insert(std::make_shared<Bead>());

Failing that, you will have to perform the dynamic allocation yourself, but thenceforth the lifetime of the object will be managed by the shared pointer:

boxes[i][j][k].insert(new Bead);

You might like to change your data structure, though, to avoid excessive use of containers. Each container incurs allocations, which may turn out to be expensive. If you need to fill 3D space densely, then you could use a flattened-out 1D view that you access in strides. If you only need sparse points, a map keyed on triples may be an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文