【问题标题】:Segmentation fault in boost::odeint my_vector.cpp exampleboost::odeint my_vector.cpp 示例中的分段错误
【发布时间】:2015-11-04 21:53:17
【问题描述】:

我正在尝试从 boost::odeint 示例中测试 my_vector.cpp 程序,但没有任何成功。运行程序时出现分段错误。 编译:g++ -std=c++14 -o my_vector my_vector.cpp

它使用 std::vector 而不是 state_type 的 my_vector 运行。我怀疑 is_resizable 有问题,但我不知道如何解决。

这里是 my_vector.cpp 的代码,您可以从 github 获得

/*
* Copyright 2011-2012 Mario Mulansky
* Copyright 2012-2013 Karsten Ahnert
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or
* copy at http://www.boost.org/LICENSE_1_0.txt)
*
* Example for self defined vector type.
*/

#include <vector>

#include <boost/numeric/odeint.hpp>

//[my_vector
template< int MAX_N >
class my_vector
{
    typedef std::vector< double > vector;

public:
    typedef vector::iterator iterator;
    typedef vector::const_iterator const_iterator;

public:
    my_vector( const size_t N )
        : m_v( N )
    { 
        m_v.reserve( MAX_N );
    }

    my_vector()
        : m_v()
    {
        m_v.reserve( MAX_N );
    }

// ... [ implement container interface ]
//]
    const double & operator[]( const size_t n ) const
    { return m_v[n]; }

    double & operator[]( const size_t n )
    { return m_v[n]; }

    iterator begin()
    { return m_v.begin(); }

    const_iterator begin() const
    { return m_v.begin(); }

    iterator end()
    { return m_v.end(); }

    const_iterator end() const
    { return m_v.end(); }

    size_t size() const 
    { return m_v.size(); }

    void resize( const size_t n )
    { m_v.resize( n ); }

private:
    std::vector< double > m_v;

};

//[my_vector_resizeable
// define my_vector as resizeable

namespace boost { namespace numeric { namespace odeint {

template<size_t N>
struct is_resizeable< my_vector<N> >
{
    typedef boost::true_type type;
    static const bool value = type::value;
};

} } }
//]


typedef my_vector<3> state_type;

void lorenz( const state_type &x , state_type &dxdt , const double t )
{
    const double sigma( 10.0 );
    const double R( 28.0 );
    const double b( 8.0 / 3.0 );

    dxdt[0] = sigma * ( x[1] - x[0] );
    dxdt[1] = R * x[0] - x[1] - x[0] * x[2];
    dxdt[2] = -b * x[2] + x[0] * x[1];
}

using namespace boost::numeric::odeint;

int main()
{
    state_type x(3);
    x[0] = 5.0 ; x[1] = 10.0 ; x[2] = 10.0;

    // my_vector works with range_algebra as it implements 
    // the required parts of a container interface
    // no further work is required

    integrate_const( runge_kutta4< state_type >() , lorenz , x , 0.0 , 10.0 , 0.1 );
}

【问题讨论】:

  • 我正在使用 g++ 4.9.3 和 boost 1.54.0

标签: c++11 boost odeint


【解决方案1】:

我可以用 g++-4.8 重现这个。

我已经在 odeint github 存储库中打开了一个关于此的问题: https://github.com/headmyshoulder/odeint-v2/issues/180

编辑:

我已经找到问题并解决了:https://github.com/headmyshoulder/odeint-v2/commit/965a8e456d5e9dff47f0121a4d12aac8c845ea5e

问题是模板参数类型不一致,导致 is_resizeable 定义无法正常工作,正如操作所期望的那样。

将 my_ 矢量模板参数类型更改为 size_t 可以解决此问题:

template<size_t N>
class my_vector

【讨论】:

  • 我仍然认为 DEBUG asserts/.at() 的使用是正确的,因为这种类型的破损很容易被忽略。
  • @sehe:同意,我添加了一个编译时检查,以确保在示例中调整大小。我认为在这种情况下这是一个很好的做法:github.com/headmyshoulder/odeint-v2/commit/…
猜你喜欢
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多