簡體   English   中英

C ++:gcc 4.2.1提供了segfaults程序,不在Linux上

[英]C++: gcc 4.2.1 gives program which segfaults, not on linux

我的運行gcc 4.2.1的mac(Lion)上的以下簡單程序segfaults:

#include <iostream>

using namespace std;
struct A{
  friend std::ostream& operator << (std::ostream& os, const A& a) {
    os << 3 << endl;
  }
};

template <typename T>
T f() { return T();}

int f() { return 2;}


int main() {
  cout << f() << endl;

  A a= f<A>();
  cout << a << endl;
}

運行程序時,我得到:

 ./a.out
2
3
Segmentation fault: 11

當我執行堆棧跟蹤時,我得到:

(gdb) run

Starting program: a.out 
unable to read unknown load command 0x24
unable to read unknown load command 0x26
2
3

Program received signal SIGSEGV, Segmentation fault.
0x00007fff8b84fa49 in ?? ()
(gdb) bt
#0  0x00007fff8b84fa49 in ?? ()
#1  0x00007fff665c1ae8 in ?? ()
#2  0x0000000000000000 in ?? ()

回溯沒有有用的信息(任何人都知道為什么嗎?)。 這在linux上工作正常。

讓我們更清楚地說明會發生什么

(cout << a) << endl;

您忘記了operator<<return

暫無
暫無

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

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