- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码给出了一个段错误:
#include <iostream>
#include <fstream>
#include "binItr.h"
#include <boost/multi_array.hpp>
using namespace std;
int main(){
const char * xifile = "results/feretxiG1155V0P5T231K10.bin";
const uint pSize = 5;
const uint T = 231;
ifstream xiFileId(xifile, ios::binary);
typedef boost::multi_array<uint, 2> array_type;
array_type xi(boost::extents[T][pSize + 1]);
//the ii_t class in the following line is taken from http://stackoverflow.com/questions/1855704/c-binary-file-i-o-to-from-containers-other-than-char-using-stl-algorithms written by http://stackoverflow.com/users/14065/loki-astari
ii_t<uint> xi_in(xiFileId);
copy(xi_in, ii_t<uint>(), xi.data());
return 0;
}
输入二进制文件包含 unsigned int
数据,ls -l
报告的大小为 231*(5+1)4 = 5544 字节。我尝试读取文件并将数据存储在 vector 中,发现 vector 大小为 231(5+1) = 1386。使用 gdb 分析核心文件给出以下输出。
Program terminated with signal 6, Aborted.
#0 0x00007fb71130ea75 in raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
in ../nptl/sysdeps/unix/sysv/linux/raise.c
(gdb) bt
#0 0x00007fb71130ea75 in raise (sig=<value optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1 0x00007fb7113125c0 in abort () at abort.c:92
#2 0x00007fb7113484fb in __libc_message (do_abort=<value optimized out>, fmt=<value optimized out>) at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
#3 0x00007fb7113525b6 in malloc_printerr (action=3, str=0x7fb711425cd8 "double free or corruption (!prev)", ptr=<value optimized out>) at malloc.c:6266
#4 0x00007fb711358e83 in __libc_free (mem=<value optimized out>) at malloc.c:3738
#5 0x00000000004018c4 in __gnu_cxx::new_allocator<unsigned int>::deallocate (this=0x7fffc618d2f8, __p=0x2295290) at /usr/include/c++/4.4/ext/new_allocator.h:95
#6 0x000000000040152f in boost::multi_array<unsigned int, 2ul, std::allocator<unsigned int> >::deallocate_space (this=0x7fffc618d290) at /usr/include/boost/multi_array.hpp:484
#7 0x0000000000401077 in boost::multi_array<unsigned int, 2ul, std::allocator<unsigned int> >::~multi_array (this=0x7fffc618d290, __in_chrg=<value optimized out>) at /usr/include/boost/multi_array.hpp:468
#8 0x0000000000400d4e in main () at segTest.cpp:30
有什么建议吗?谢谢。
最佳答案
问题是 ii_t<>
来自 referred SO answer 的输入迭代器类正在“阅读”太多项目,因为包装 istream
不返回 EOF
直到在返回文件中最后一项的迭代器之后 取消引用。额外返回的数据项正在破坏 multi_array
中分配的内存块。对象。
如果您更改 ii_t<>
类如下,你应该得到更好的行为:
template<typename T>
struct ii_t: public iterator<input_iterator_tag, void, void, void, void>
{
ii_t(std::istream& str)
:m_str(&str)
{}
ii_t()
:m_str(NULL)
{}
ii_t& operator++() {return *this;} // increment does nothing.
ii_t& operator++(int){return *this;}
T& operator*()
{
// On the de-reference we actuall read the data into a local //// static ////
// Thus we can return a reference
static T result;
m_str->read(reinterpret_cast<char*>(&result),sizeof(T));
return result;
}
// If either iterator has a NULL pointer then it is the end() of stream iterator.
// Input iterators are only equal if they have read past the end of stream.
bool operator!=(ii_t const& rhs)
{
// we need to make sure we're not just about to hit EOF
// if we already haven't
if (m_str && m_str->good()) {
char dummy;
m_str->read(&dummy,1);
if (m_str->good()) {
m_str->putback(dummy);
}
}
if (rhs.m_str && rhs.m_str->good()) {
char dummy;
rhs.m_str->read(&dummy,1);
if (rhs.m_str->good()) {
rhs.m_str->putback(dummy);
}
}
bool lhsPastEnd = (m_str == NULL) || (!m_str->good());
bool rhsPastEnd = (rhs.m_str == NULL) || (!rhs.m_str->good());
return !(lhsPastEnd && rhsPastEnd);
}
private:
std::istream* m_str;
};
相关变化在bool operator!=(ii_t const& rhs)
中函数,如有必要,在包装的 istream
上执行(然后撤消)虚拟读取确定 istream 是否在 EOF。
请注意,我并没有声称这是处理 EOF
的最佳技术。情况,但它似乎有效。
关于c++ - boost::multi_array 上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8004456/
我一直在研究 boost::multi_array 库,以寻找一个允许您在单个 for 循环中遍历整个 multi_array 的迭代器。 我不认为那个库中有任何这样的迭代器。 (在那里找到的迭代器可
我有一个 3 维的 boost::multi_array boost::multi_array* newArr = new boost::multi_array(boost::extents[x][
下面的代码给出了一个段错误: #include #include #include "binItr.h" #include using namespace std; int main(){
我面临以下问题。我想使用 boost::multi_array 创建一个对象指针的多维数组,但即使我编写的代码可以编译,当我尝试在 Eclipse 中运行时,程序也会终止并且不会打印任何内容。让我举例
我对我认为应该是一段简单的代码有疑问。我有一个 N x M 的二维数组,当前存储在 boost multi_array 中。 N 列表示空间维度,例如x,y,z 和 M 行是每个维度上的点。 我想做的
我有一个模板类,它执行一些计算并返回 multi_array,像这样有点过于简单了: template class C { public: typedef boost::multi_arra
我有三维 boost::multi_array 对象。我想将 origin 的绝对偏移量转换为多维索引,反之亦然。有没有一种简单的方法可以使用 boost 的内置工具来做到这一点,或者我需要自己根据数
我想学习如何将一个一维 multi_array 添加到另一个一维 multi_array 的末尾。我该怎么做? 最佳答案 与任何其他容器一样,Boost 多数组具有(没有很好记录的)迭代器,因此您可以
我正在使用 boost::multi_array 来存储一些数据。我使用 View 处理数据,因为我需要处理不同维度的数据切片。 我的问题是,boost::multi_array 的内存是如何管理的?
我正在使用二维 boost::multi_array 来存储自定义结构的对象。问题是我有大量的这些对象,所以我需要的数组索引超出了整数的范围。是否有可能将 long 用作多数组的索引,或者您对如何存储
我看过this post它解决了如何使用 boost::multi_array::origin() 函数循环遍历不是从零开始的数组,但这只会创建一个循环。 如何遍历multi_array的每一维,例如
范围可用于对 Boost 多维数组 (multi_array) 进行切片。根据documentation有多种定义范围的方法,但并非所有方法都能编译。我在 Ubuntu 11.04 上使用 GCC 4
假设我有一个 N 维 boost::multi_array(为简单起见,类型为 int),其中 N 在编译时已知,但可以变化(即是一个非类型模板参数).我们假设所有维度的大小都相同 m。 typede
首先我想说我是新手。 我正在尝试初始化 boost:multi_array在我的类(class)里。我知道如何创建 boost:multi_array : boost::multi_array foo
在下面的代码中,ExtractSubArray 函数是完全通用的,而 ExtractSubArrayCornerAndExtent 需要在编写代码时了解维度(以构造RangeType 参数)。有什么方
我有一个 3 维 boost::multi_array 表示 2d tilemaps 层。我希望能够清除一层——也就是遍历一层上的所有图 block 并将其值设置为 0,但我不知道该怎么做——我相信我
我正在尝试弄清楚 boost::multi_array 构造函数或调整大小方法是否可以抛出 bad_alloc 异常(或指示分配或调整大小失败的其他一些异常)。我无法在任何地方的文档中找到此信息。 澄
我必须通过引用或指针将数组传递给其他函数,我不在乎,只要它运行速度快即可。这就是我开始使用boost库的原因。我是通过以下方式做到的: using namespace boost; typedef
我无法调整 boost::multi_array 的大小。当我尝试它时,它会给出关于 std::_Copy_impl 等的错误。这是代码 #include typedef boost::multi_
我正在尝试使用以下测试程序将 boost::multi_array 的性能与本地动态分配的数组进行比较: #include #define _SCL_SECURE_NO_WARNINGS #defi
我是一名优秀的程序员,十分优秀!