在静态变量初始化中使用cout时发生C ++分段错误 [英] C++ Segmentation Fault when using cout in static variable initialization

查看:423
本文介绍了在静态变量初始化中使用cout时发生C ++分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用cout发出调试信息的程序。该代码在静态全局变量的初始化中执行,即在程序执行的早期。当我使用自己的构建脚本来构建程序时,在第一次使用cout时它会出现段错误(仅将字符串文字移入cout中,因此它不能是值)。我使用valgrind来检查对无效位置的较早写入,但没有写入(也没有代码可能会生成这些写入,在输出之前我没有做太多事情)。当我将源代码复制到eclipse项目并让eclipse内置构建器对其进行构建时,一切正常。我没有使用仅用 -ggdb -std = c ++ 0x 编译的奇怪的构建器设置,这是仅有的两个标志。

I have a program where I use cout to emit debug information. The code is executed in the initialization of a static global variable, i.e. quite early in the program execution. When I use my own build script to build the program, it segfaults at the first use of cout (only a string literal is shifted into cout, so it cannot be the value). I used valgrind to check for earlier writes to invalid locations, but there are none (and there is also no code that would be likely to generate those writes, I dont do too much before the output). When I copy the source code to an eclipse project and let the eclipse built-in builder build it, then everything works fine. I used no weird builder settings simply compiled with -ggdb -std=c++0x, these are the only two flags.

那么,如果以前没有无效写入,则带有字符串文字段错误的cout可能是什么原因?

So what can be the reason that a cout with a string literal segfaults, if there were no invalid writes before? How can the build configuration affect this?

(很抱歉,我不能给您任何最小的示例,因为该示例将在您的计算机上很好地编译,因为它可以我使用eclipse构建器时)

(I am sorry I can give you no minimal example, as this example would simply compile fine at your machine, as it does for me when using the eclipse builder)

编辑:以下是堆栈跟踪:

Here is the stacktrace:

0x00007ffff7b6d7d1 in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib   /x86_64-linux-gnu/libstdc++.so.6
(gdb) backtrace
#0  0x00007ffff7b6d7d1 in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007ffff7b6dee9 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff7b6e2ef in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) ()
  from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00000000004021be inTest::fill (this=0x6120f8, funcs=...) at inTest.cpp:92

最后一帧是我的代码。第92行的内容为:

The last frame is my code. Line 92 simply reads:

std::cout << "Test";


推荐答案

正如Luchian指出的那样,您不能使用<$在构造 ios_base :: Init 的第一个
实例之前,先创建c $ c> std :: cout 。不过,您不必
定义实例;包括< iostream> 就足够了。

As Luchian has pointed out, you cannot use std::cout before the first instance of ios_base::Init has been constructed. You don't have to define an instance, however; including <iostream> should be enough.

初始化顺序定义在一个翻译单元。
如果在具有静态
实例的所有文件的顶部包含< iostream> ,则应该可以。但是,如果静态对象
的构造函数在另一个转换单元中调用一个函数,而该转换单元中的输出为
,则不足以包括< iostream>。
仅在执行输出的翻译单元中。您必须将其
包含在定义了静态变量的转换单元中。如果他们不做任何输出,甚至

Order of initialization is defined within a single translation unit. If you include <iostream> at the top of all files which have static instances, you should be OK. If the constructor of a static object calls a function in another translation unit, however, and the output is in that translation unit, it is not sufficient to include <iostream> only in the translation unit which does the output. You must include it in the translation unit where the static variable(s) are defined. Even if they don't do any output.

这篇关于在静态变量初始化中使用cout时发生C ++分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆