2015-11-04 52 views
2

我想從boost :: odeint示例中測試my_vector.cpp程序,但沒有任何成功。當我運行程序時出現分段錯誤。 編譯:g ++ -std = C++ 14 -o my_vector my_vector.cppboost :: odeint my_vector.cpp示例中的分段錯誤示例

它使用std :: vector <>代替my_vector運行state_type。我懷疑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); 
} 
+0

我使用G ++ 4.9.3和促進1.54.0 – Bociek

回答

2

我可以重現這與G ++ - 4.8。

我在odeint GitHub的倉庫在此開設了一個問題: https://github.com/headmyshoulder/odeint-v2/issues/180

編輯:

我發現問題並解決它:https://github.com/headmyshoulder/odeint-v2/commit/965a8e456d5e9dff47f0121a4d12aac8c845ea5e

的問題是不一致的模板參數類型阻止is_resizeable定義正常工作,正如op所預期的那樣。

改變MY_矢量模板參數類型爲size_t修復了這個:

template<size_t N> 
class my_vector 
+0

我仍然認爲,DEBUG斷言/ .AT()的用法將是爲了,因爲這種破損很容易錯過。 – sehe

+0

它適用於我:) – Bociek

+0

@sehe:同意,我添加了一個編譯時檢查,確保調整大小是在示例。我認爲在這種情況下這是一個很好的做法:https://github.com/headmyshoulder/odeint-v2/commit/f71de757bac1677934e998fda785f91fa439127f – mariomulansky