加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – std :: vector crash

发布时间:2020-12-16 05:35:25 所属栏目:百科 来源:网络整理
导读:这个问题是 my question的延续. 这是有问题的代码. 啊: #include string#include vectorstd::vectorstd::string foo(); a.cpp #include "a.h"std::vectorstd::string foo(){ std::vectorstd::string v; return v;} 最后是main.cpp: #include "a.h"#include
这个问题是 my question的延续.

这是有问题的代码.

啊:

#include <string>
#include <vector>

std::vector<std::string> foo();

a.cpp

#include "a.h"

std::vector<std::string> foo()
{
   std::vector<std::string> v;
   return v;
}

最后是main.cpp:

#include "a.h"
#include <iostream>

int main()
{
    std::vector<std::string> s = foo();

    return 0;
}

编译如下(main.cpp用STL调试标志编译):

g++ -c a.cpp
g++ -D_GLIBCXX_DEBUG main.cpp a.o

运行a.out时,进程崩溃:

Core was generated by `./a.out'.
Program terminated with signal 11,Segmentation fault.
#0  0x00007fe355998c43 in __gnu_debug::_Safe_iterator_base::_M_detach_single() () from /usr/lib64/libstdc++.so.6
(gdb) bt
#0  0x00007fe355998c43 in __gnu_debug::_Safe_iterator_base::_M_detach_single() () from /usr/lib64/libstdc++.so.6
#1  0x00007fe355999ebc in __gnu_debug::_Safe_sequence_base::_M_detach_all() () from /usr/lib64/libstdc++.so.6
#2  0x0000000000400cac in __gnu_debug::_Safe_sequence_base::~_Safe_sequence_base() ()
#3  0x0000000000400cc6 in __gnu_debug::_Safe_sequence<std::__debug::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::allocator<char> > > > >::~_Safe_sequence() ()
#4  0x0000000000400ce7 in std::__debug::vector<std::basic_string<char,std::allocator<char> > > >::~vector() ()
#5  0x0000000000400c35 in main ()

我的gcc:

Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.4 --enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --program-suffix=-4.4 --enable-linux-futex --without-system-libunwind --with-arch-32=i586 --with-tune=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 4.4.1 [gcc-4_4-branch revision 150839] (SUSE Linux)

解决方法

在上一个问题中,您可以在此处引用GCC文档: http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt03ch17s04.html.该文档指出GCC libstdc“支持按用户重新编译”,并将其定义如下:

Per-use recompilation: The user must recompile the parts of his or her application and the C++ libraries it depends on where debugging should occur,and any other code that interacts with those containers. This means that a set of translation units that accesses a particular standard container instance may either be compiled in release mode (no checking) or debug mode (full checking),but must all be compiled in the same way; a translation unit that does not see that standard container instance need not be recompiled. This also means that a translation unit A that contains a particular instantiation (say,std::vector) compiled in release mode can be linked against a translation unit B that contains the same instantiation compiled in debug mode (a feature not present with partial recompilation). While this behavior is technically a violation of the One Definition Rule,this ability tends to be very important in practice. The libstdc++ debug mode supports this level of recompilation.

对于每个单元的编译,这是你在这里尝试做的,它说:

We believe that this level of recompilation is in fact not possible if we intend to supply safe iterators,leave the program semantics unchanged,and not regress in performance under release mode….

因此,我对你上一个问题的回答并不完全准确,我道歉.我已经添加了一个附录来纠正它,我希望这是一个有用的建议,如何解决那里的多库问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读