c++ - g++ 在正常编译时会产生段错误,但在 -g 时不会

标签 c++ g++ segmentation-fault

我现在正在使用 Bruce Eckel 的“Thinking in C++”学习 C++,并且我在学习前面几章。我有 C 和 Java 背景。现在我遇到了以下问题:当我用

编译下面的源代码时
g++ A.cpp B.cpp bmain.cpp

,程序输出“1”(正确),然后是段错误。当我编译时

g++ -g A.cpp B.cpp bmain.cpp

,完全相同的程序产生 1 且没有段错误!我不得不说我发现这令人惊讶。有人可以指出我做错了什么吗?我的操作系统是“Linux 2.6.35-30-generic#54-Ubuntu x86_64”,我的 g++ 版本是“g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5”。

编辑:仅仅因为这似乎是错误的重要来源,感谢@Evan Teran:永远不会调用 B 结构中的 A 构造函数!我放了一个“cout << “blah” << endl;”里面,它不打印任何东西

编辑:我现在在 main 的末尾包含了“return 0”,但这没有帮助。

嗯:

#ifndef A_H
#define A_H

#include <string>

class A {
public:
        int i;
        std::string str;
        void print();
        A();
};

#endif

A.cpp:

#include "A.h"
#include <iostream>
#include <string>

using namespace std;

void A::print() {
        cout << str << " " << i << endl;
}

A::A() {
        str = "initstr";
    i = 0;
}

B.h:

#ifndef B_H
#define B_H

#include "A.h"

class B {
private:
        int counter;
public:
        A a;
        B();
        void increase();
        int read();
};

#endif

B.cpp:

#include "B.h"

using namespace std;

B::B() {
        counter = 0;
}

void B::increase() {
        ++counter;
}

int B::read() {
        return counter;
}

bmain.cpp:

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

using namespace std;

int main(int argc, char **argv) {
        B b;
        b.increase();
        cout << b.read() << endl;
        return 0;
}

编辑:我已经从软件包中安装了 g++。我的 Ubuntu 也很标准。

这是我打电话时得到的 gdb a.out 核心

warning: Can't read pathname for load map: Eingabe-/Ausgabefehler.
Reading symbols from /usr/lib/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libstdc++.so.6
Reading symbols from /lib/libm.so.6...Reading symbols from /usr/lib/debug/lib/libm-2.12.1.so...done.
done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...Reading symbols from /usr/lib/debug/lib/libc-2.12.1.so...done.
done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug/lib/ld-2.12.1.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007fba1049104b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /usr/lib/libstdc++.so.6

编辑 2: 顺便说一句,据我所知,我的硬件没有故障,我对操作系统的处理也很好

编辑 3: Valgrind 报告如下:

==3428== Conditional jump or move depends on uninitialised value(s)
==3428==    at 0x4ECB022: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428== 
==3428== Use of uninitialised value of size 8
==3428==    at 0x4ECB04B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428== 
==3428== Invalid read of size 4
==3428==    at 0x4ECB04B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==  Address 0xfffffffffffffff8 is not stack'd, malloc'd or (recently) free'd
==3428== 
==3428== 
==3428== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==3428==  Access not within mapped region at address 0xFFFFFFFFFFFFFFF8
==3428==    at 0x4ECB04B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==  If you believe this happened as a result of a stack
==3428==  overflow in your program's main thread (unlikely but
==3428==  possible), you can try to increase the size of the
==3428==  main thread stack using the --main-stacksize= flag.
==3428==  The main thread stack size used in this run was 8388608.
==3428== 
==3428== HEAP SUMMARY:
==3428==     in use at exit: 0 bytes in 0 blocks
==3428==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3428== 
==3428== All heap blocks were freed -- no leaks are possible    

最佳答案

按照这种方式,它似乎是 GCC/libstdc++ 打包/构建或所用版本中的错误。试试 GCC 4.5 或 4.6,如果没有,请告诉自己始终使用最新和最好的(当然直到它破坏某些东西)并且永远不要回头。

似乎编译器没有初始化 B 中的 A 成员,这将导致 std::string 的析构函数失败阅读必要的信息以正确销毁自身。但这只是推测和猜测。

关于c++ - g++ 在正常编译时会产生段错误,但在 -g 时不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6707730/

相关文章:

c++ - 在 Gtkmm 中创建自定义 IconFactory

c++ - 我能否使 Visual Studio 2008 中的 C++ 表现得像早期版本一样?

c++ - 尝试读取第二个多维数组时发生段错误

c - 为什么段错误(核心转储)?

c++ - 为什么在此代码中出现运行时错误SIGSEGV

c++ - 从非模板基类扩展并覆盖函数参数的模板类

python - 如何使 C 枚举类型可用于其他语言?

c++ - 为什么 std::seed_seq 根据 C++11 是不可复制的,为什么 gcc/clang 不符合?

c++ - [linux][ompl]添加库路径

gcc - CentOS 7 升级了 gcc/g++ 但似乎无法使用?