繁体   English   中英

来自C ++参考中用于shared_ptr的示例的分段错误

[英]Segmentation fault from example in C++ reference for shared_ptr

我从http://en.cppreference.com/w/cpp/memory/shared_ptr中获取了示例代码,并在本地对其进行了编译,如下所示:

g++ -std=c++0x -D_GLIBCXX_USE_NANOSLEEP my_file.cpp 

运行它时,得到以下输出:

  Base::Base()
  Derived::Derived()
Created a shared Derived (as a pointer to Base)
  p.get() = 0x608029, p.use_count() = 1
Segmentation fault

知道为什么会有段错误吗?

我是C ++的新手,您通常如何调试它?

谢谢!

如果您正使用链接页面上的源代码,建议对命令行进行以下更改:

  1. 使用-std=c++11-std=c++14
  2. 启用pthread库的使用并使编译器具有线程意识。
g++ -Wall -std=c++11  -pthread my_file.cpp
g++ -Wall -std=c++14  -pthread my_file.cpp

该示例遭受“基类在基类上没有虚拟析构函数”的困扰。 当使用指向Base的指针销毁Derived对象时,代码仅调用Base的析构函数。 这是一件坏事(TM)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM