问题标签 [boost-ptr-container]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
827 浏览

c++ - std::map 和 boost::ptr_map 模板和继承的返回值变坏

在我工作的公司,我们创建了一个名为“RestrictedMap”的类。这提供了与常规 std::map 相同的接口,但不允许您使用 [] 运算符。还提供了一些其他功能以舒适地使用该类。该类在内部包装了一个 std::map。

我现在正在尝试创建一个类似的类,它对 boost::ptr_map 执行相同的操作,称为“RestrictedPointerMap”。为此,我创建了RestrictedMapBase,它接受作为模板参数的映射类型,它应该包装并包含大部分实现。两个类派生它并指定要包装的映射类型:

  • RestrictedMap与我们以前拥有的相同并包装 std::map
  • RestrictedPointerMap是新的并包装了 boost::ptr_map。

这是代码,为了完整起见,我没有简化类,但我稍后会命名相关函数。

受限地图.h

这主要是有效的,除非我想在 RestrictedPointerMap 上调用getValue 。函数getData返回正确的值,但之后在getValue函数中出错。它返回一个错误的指针(如指针错误)。

这是一些重现该问题的代码:

测试类.h

测试类.cpp

TestRestrictedPtrMap.cpp(主要)

这个的输出是:

我不知道为什么会出错。据我所知,返回值变坏了。

提前感谢您的帮助,

汤姆

0 投票
1 回答
112 浏览

c++ - 当 ptr_vector 迭代器失效时

插入和/或删除元素是否有可能使现有元素的迭代器无效。

谢谢你。

0 投票
0 回答
113 浏览

c++ - Puzzling segfault when inserting to ptr_map

I have been pulling my hair for a few hours trying to understand a puzzling segfault. The exact same code works in my object constructor but not in the object's method where it's supposed to be: I get a segfault at run time.

Hopefully, I have not snipped anything relevant:

stud.hpp:

stud.cpp:

backtrace:

Request: solve my problem for me! ;)

Now, more seriously:
Questions:
does the ptr_map people need to be initialized in any way in the constructor?
Why would the same insert code work in the constructor but not in the method?
What can cause a ptr_map insert to segfault the application?

Edit:

The application is actually a plugin for an cppcms-based application. The whole application is already starting to get fairly complex and I cannot reasonably post everything. However, what I am seeing in my IDE is more or less what I wrote above and that's why I am as perplexed as you seem to be (or the other way around!). I am using hard-coded values for debugging, not relying on the id passed to the method. If I comment out the insert line, the application runs fine. The std::cout above it print, but the one below doesn't. The backtrace says something about std::less that I don't understand. I did my best to summarize the problem. I have already spent many hours on it and I am truly perplexed. I appreciate your understanding and your help. If need be, I'll think about more drastic, time-consuming ways to debug the application... Meanwhile...

0 投票
1 回答
472 浏览

c++ - 测量向量的性能在 VS2013 上?

TL;DR VS2013 的优化器是否混淆了,或者我的测量结果是否错误,或者全局虚拟对象实际上是否需要易失性才能使测试有效或____?

免责声明:这主要是出于“学术”兴趣,我不认为我看到的差异会真正影响任何生产代码。


简介:我最近的一些测量结果让我想到了这个问题,因为我看到了 VS2013之间std::vector<std::unique_ptr<T> >的显着差异。boost::ptr_vector(另见那里的评论)

对于我的特定测试用例,访问 boost::ptr_vector 中的元素似乎比使用 unique_ptr 的向量快 50%!

我的测试代码在这里:http ://coliru.stacked-crooked.com/a/27dc2f1b91380cca (我将避免在这个问题中也包括它,我将在下面包含片段)

  • gcc 4.8 没有报告任何差异,所以这是 VS2013 的事情。

    /li>
  • 我对链接到的测试代码的确切时间是:

    /li>

测试循环看起来像这样,我还将在那里保留冗长的注释来解释我所看到的:

如果循环中唯一的一行是访问元素(将 ptr 放入全局虚拟对象中),那么 VS2013 优化器似乎做了一些奇怪的事情。当if (pGlobalDummy)存在时,两种情况都是相同的。

任何人都可以分享一些关于这一点的信息吗?

感谢霍华德的回答,我确实发现添加volatile到全局虚拟会有所不同,即当全局虚拟像这样不稳定时:

循环运行速度稍慢,但运行完全相同。volatile 应该在这里有所作为吗?也就是说,如果没有 volatile,测试是否有效?

0 投票
2 回答
297 浏览

c++ - std::reverse 在 boost::ptr_vector 切片对象上?

BaseDerived成为具有数据成员的类:

现在在堆上创建 和 的一些实例Base并将Derived它们存储在 a 中boost::ptr_vector

打印所有对象:

然后反转并再次打印:

该程序打印:

std::reverse()onboost::ptr_vector调用std::iter_swap()又调用std::swapwhich 通过创建临时副本来交换项目。

但是,临时副本确实对 Derived 对象进行了切片。如您所见,int g对象Derived1 和 4 的没有交换,因此交换后对象已损坏。

这种行为让我很困惑。容器不boost::ptr_vector就是要避免这种问题吗?

我在这里需要的是一个虚拟复制构造函数,它在 C++ 中不存在。

我该如何解决这个问题?我是否需要为 实现一个虚拟交换函数Base,以便虚拟调度调用另一个交换成员函数Derived

编辑:为什么首先要复制对象?由于向量只存储指针,因此反转它应该是指针的交换,而不是指向的对象!有没有这样做的功能?

0 投票
0 回答
316 浏览

c++ - 删除时 boost::ptr_vector 分段错误 (munmap_chunk())

我正在使用 boost::ptr_vector 来包含类对象的列表。

我以这种方式将对象添加到 ptr_vector 中:

当初始化包含 ptr_vector 的类时,我只添加一次对象。

我从不复制向量。

我不会在向量中添加或删除对象,但我确实以这种方式对 ptr_vector 中的元素调用 setCoords 函数:

我的问题是,当包含 ptr_vector 的类被破坏时,我似乎间歇性地遇到了分段错误。当我在运行期间对许多不同的元素调用 setCoords 时,似乎更有可能发生故障。

我收到的消息是:

非常感谢您的帮助,我在下面粘贴了 gdb 回溯:

0 投票
1 回答
124 浏览

c++ - Increasing Speed of Destruction

I have an application that creates thousands of small objects (upwards of 500,000). There is an object factory that allocates these objects on the heap.

The problem that I'm running into is that when the object that holds these smaller object goes out of scope (Driver), 65% of the processing time is spent destroying these small objects.

map, entries, and fields hold pointers to an abstract base class, and each base class has many child classes.

The application architecture follows this format:

I have tried several different methods to increase the application's performance.

I first used a data structures with normal pointers and then explicitly deleted the objects in the class's destructor.

I then tried using data structures with boost::shared_ptr<> but I determined that reference counting caused a significant overhead and didn't provide any great benefit.

The solution I've come to now is to use boost::ptr_container so it takes ownership of the heap objects and will automatically destroy them when the container goes out of scope. With this solution, significant time is still spent destroying objects.

Is there anything I can do to prevent all this time destroying objects?

0 投票
1 回答
136 浏览

c++ - 从源代码编译后未安装 boost ptr_container 库

我已将 boost 库从以前的 1.54(svn 源)更新到 1.57(git 源)。虽然我使用了相同的 ./b2 参数,但目标目录不包含ptr_container库。

具有克隆存储库的目录正确包含此路径上的 ptr_container 库:

我正在使用以下命令构建它:

但没有成功。


编辑:

似乎只有从 git 构建时才会出现问题。当我从 boost 站点下载 zip 包时,目标目录正确包含 ptr_container。

这是我用来获取源代码的 git 命令:

我也尝试获取最新的 boost 版本,但这个版本根本不可用。在我执行 ./b2 之后,我得到了类似于这个的奇怪错误:Buidling boost error: Name conflict for '<pstage\lib>boost_system-vc120-mt-1_58.dll'

对于如何从 git 存储库正确获取和安装 boost 的任何提示,我将不胜感激。

0 投票
1 回答
63 浏览

c++ - 如何安全地填充 Boosts 的指针容器?

Boost Pointer Container的第一个示例添加了一个指向结构的原始指针:

但是如果push_back或任何其他可能触发重新分配的成员函数在此过程中抛出异常怎么办?据我了解,在这种情况下,由调用者来管理给定对象的内存,但是由于调用者将原始指针传递给旨在管理内存的类,因此调用者很可能不会吨。

那么,是否不需要在上面的代码示例中使用某种唯一的智能指针来包装指针,然后再将其提供给容器并确保没有内存泄漏?容器确实为此类智能指针提供了重载,但它们不会强制使用它们。

或者是容器在任何添加操作期间根本不会抛出异常并且它总是成功的论点?

0 投票
1 回答
131 浏览

c++11 - std::unordered_map < K, boost::ptr_deque < T > > 的 operator[] (K const &) 和 emplace 的区别

在上面的示例中,注释掉的行不会编译,因为它们试图复制ptr_deque不可复制的元素。但是,push_back表格有效。

我在想那operator [] (K const &)只是return emplace(k, mapped_type()).first->secondor return insert(value_type(k, mapped_type())).first->second,本质上是注释掉的语句

显然情况并非如此。是否在内部operator []执行一些placement new魔术?

还是有什么特别之处ptr_deque

我正在使用 gcc-6.1 & boost 1.59