簡體   English   中英

g++ 在正常編譯時會產生段錯誤,但在使用 -g 時不會產生段錯誤

[英]g++ produces segfault with normal compilation, but none with -g

我現在正在使用 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 和 NO 段錯誤。 我不得不說我覺得這很驚人? 有人可以指出我做錯了什么。 我的操作系統是“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;
}

:

#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 core 時得到的

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的析構函數無法讀取必要的信息以正確自毀。 但這只是推測和猜測。

您幾乎可以肯定在程序的某處調用了未定義的行為(UB)。 UB 的重點是行為不僅是未定義的,而且可能會根據平台、編譯器、標志等而改變。添加-g可能會擾亂事物以避免段錯誤,但這只是機會。

在 main() 中添加 return 語句。 我很驚訝你的編譯器沒有警告你。

Function 缺少返回值,運行時行為

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM