Tôi đã làm việc trên chương trình của mình và tôi đã quyết định bật một số tối ưu hóa bằng cách sử dụng g++ -O3
. Đột nhiên, chương trình của tôi bắt đầu segfaulting. Tôi đã tìm kiếm mã có vấn đề và giảm thiểu chương trình của tôi thành một chương trình nào đó vẫn còn bị phân đoạn (chỉ khi sử dụng tối ưu hóa mức 3). Tôi đã hy vọng ai đó có thể mất một peek nhanh chóng vào mã (Tôi đã cố gắng giảm thiểu nó càng nhiều càng tốt):Bật tối ưu hóa g ++ gây ra segfault - Tôi không nhận được nó
// src/main.cpp
#include "rt/lights/point.hpp"
int main(int argc, char **argv)
{
rt::Light *light = new rt::light::Point(alg::vector(.0f, 5.0f, 5.0f), rt::Color(1.0f), .5f);
return 0;
}
// include/rt/lights/point.hpp
#ifndef RT_LIGHT_POINT_HPP_
#define RT_LIGHT_POINT_HPP_
#include "rt/accelerator.hpp"
#include "rt/color.hpp"
#include "rt/intersection.hpp"
#include "rt/light.hpp" // abstract
namespace rt {
namespace light {
class Point : public Light
{
public:
Point(alg::vector pos, Color color, float intensity) : Light(intensity * color), pos(pos) {}
Color get_contrib(const Intersection&, const Accelerator&, const alg::vector& toViewer) const;
private:
alg::vector pos;
};
} // namespace light
} // namespace rt
#endif
// include/rt/light.hpp
#ifndef RT_LIGHT_HPP_
#define RT_LIGHT_HPP_
#include "algebra/vector.hpp"
#include "rt/color.hpp"
namespace rt {
class Intersection;
class Accelerator;
class Light
{
public:
Light(Color intensity) : intensity(intensity) {}
virtual Color get_contrib(const Intersection&, const Accelerator&, const alg::vector& toViewer) const = 0;
Color get_intensity() const {return intensity;}
protected:
Color intensity;
};
} // namespace rt
#endif
Tôi rất thích một số cái nhìn sâu sắc về lý do tại sao mã này chỉ segfaults khi sử dụng tối ưu hóa, và làm thế nào để ngăn chặn nó làm như vậy. Cảm ơn!
$ find src/ -name "*.cpp" | xargs g++ -I include/ -O3
$ ./a.out
Segmentation fault
Edit: Theo yêu cầu, các nhà thầu cho alg :: vector
struct vector { float x, y, z; vector() : x(.0f), y(.0f), z(.0f) {} explicit vector(float f) : x(f), y(f), z(f) {} vector(float x, float y, float z) : x(x), y(y), z(z) {} // ...
Edit2: Thêm đầu ra gdb khi biên dịch với -g
(gdb) file a.out Reading symbols from /home/rob/devel/gbug/a.out...done. (gdb) run Starting program: /home/rob/devel/gbug/a.out Program received signal SIGSEGV, Segmentation fault. rt::light::Point::Point (this=0x804b008, pos=..., color=..., intensity=0.5) at src/rt/lights/point.cpp:13 13 Point::Point(alg::vector pos, Color color, float intensity) : Light(intensity * color), pos(pos) (gdb) bt #0 rt::light::Point::Point (this=0x804b008, pos=..., color=..., intensity=0.5) at src/rt/lights/point.cpp:13 #1 0x08048898 in main (argc=1, argv=0xbffff3e4) at src/main.cpp:5
Edit3: Nguồn cho rt :: Màu .
// include/rt/color.hpp #ifndef RT_COLOR_HPP_ #define RT_COLOR_HPP_ #include "algebra/vector.hpp" namespace rt { /******************************************************************************* * CLASS DEFINITION */ struct Color { float r, g, b; Color() : r(.0f), g(.0f), b(.0f) {} explicit Color(float f) : r(f), g(f), b(f) {} Color(float r, float g, float b) : r(r), g(g), b(b) {} Color& operator+= (const Color&); Color& operator*= (const Color&); Color& operator*= (float); }; /******************************************************************************* * MEMBER OPERATORS */ inline Color& Color::operator+= (const Color& other) { r += other.r; g += other.g; b += other.b; return *this; } inline Color& Color::operator*= (const Color& other) { r *= other.r; g *= other.g; b *= other.b; return *this; } inline Color& Color::operator*= (float f) { r *= f; g *= f; b *= f; } /******************************************************************************* * ADDITIONAL OPERATORS */ inline Color operator+ (Color lhs, const Color& rhs) { return lhs += rhs; } inline Color operator* (Color lhs, const Color& rhs) { return lhs *= rhs; } inline Color operator* (Color c, float f) { return c *= f; } inline Color operator* (float f, Color c) { return c *= f; } } // namespace rt #endif
Bạn đã thử biên dịch bằng -g để xem liệu có thể sử dụng backtrace không? –
Tôi tò mò tại sao bạn không biên dịch với 'g ++ src/*. Cpp -Iinclude -O3'. Với cách tiếp cận 'xargs', trình biên dịch sẽ xây dựng mỗi tệp thành' a.out', vì vậy tôi giả sử tất cả chúng đều có 'main()'. – chrisaycock
bạn có thể đăng các nhà xây dựng bản sao của 'alg :: vector'? – Naveen