2012-07-04 14 views
5

どうしてですか?コードは動作し、LinuxではGCC 4.7、WindowsではMSVC++ 2010では警告が生成されません。それでも、ideone.comではcrashesSIGILLです。未定義のビヘイビアはここに含まれますか?このC++コードがideone.comでSIGILLを取得するのはなぜですか?

#include <iostream> 
#include <cstdarg> 

using namespace std; 

enum types 
{ 
    INT, 
    DOUBLE, 
    CHAR, 
    STRING 
}; 

struct mt 
{ 
    types type; 

    union 
    { 
     int   i; 
     double  d; 
     char  c; 
     const char *s; 
    } val; 

    mt(int i) 
     : type(INT) 
    { 
     val.i = i; 
    } 

    mt(double d) 
     : type(DOUBLE) 
    { 
     val.d = d; 
    } 

    mt(char c) 
     : type(CHAR) 
    { 
     val.c = c; 
    } 

    mt(const char *s) 
     : type(STRING) 
    { 
     val.s = s; 
    } 
}; 

void print(int n, ...) 
{ 
    va_list ap; 

    va_start(ap, n); 

    for (int i = 0; i < n; i++) 
    { 
     mt x(va_arg(ap, mt)); 

     switch (x.type) 
     { 
     case INT: 
      cout << x.val.i << endl; 
      break; 
     case DOUBLE: 
      cout << x.val.d << endl; 
      break; 
     case CHAR: 
      cout << x.val.c << endl; 
      break; 
     case STRING: 
      cout << x.val.s << endl; 
      break; 
     } 
    } 

    va_end(ap); 
} 

int main() 
{ 
    print(4, mt(2), mt(4.2), mt('a'), mt("Hello")); 
} 

答えて

1

は、私はGCC 4.4.6でエラーを得た:関数のパラメータを可変引数を通じてあなたがstruct Sを渡すことはできません

test.cpp: In function ‘void print(int, ...)’: 
test.cpp:59: warning: cannot receive objects of non-POD type ‘struct mt’ through ‘...’; call will abort at runtime 
test.cpp: In function ‘int main()’: 
test.cpp:83: warning: cannot pass objects of non-POD type ‘struct mt’ through ‘...’; call will abort at runtime 
test.cpp:83: warning: cannot pass objects of non-POD type ‘struct mt’ through ‘...’; call will abort at runtime 
test.cpp:83: warning: cannot pass objects of non-POD type ‘struct mt’ through ‘...’; call will abort at runtime 
test.cpp:83: warning: cannot pass objects of non-POD type ‘struct mt’ through ‘...’; call will abort at runtime 
$ g++ --version 
g++ (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3) 

、代わりに渡すポインタ:私のシステムで正常に動作します

void print(int n, ...) 
{ 
    va_list ap; 

    va_start(ap, n); 

    for (int i = 0; i < n; i++) 
    { 
     mt *x(va_arg(ap, mt *)); 

     switch (x->type) 
     { 
     case INT: 
      cout << x->val.i << endl; 
      break; 
     case DOUBLE: 
      cout << x->val.d << endl; 
      break; 
     case CHAR: 
      cout << x->val.c << endl; 
      break; 
     case STRING: 
      cout << x->val.s << endl; 
      break; 
     } 
    } 

    va_end(ap); 
} 

int main() 
{ 
    mt mt1(2); 
    mt mt2(4.2); 
    mt mt3('a'); 
    mt mt4("Hello"); 
    print(4, &mt1, &mt2, &mt3, &mt4); 
} 

$ ./a.out 
2 
4.2 
a 
Hello 
+0

私はPOD(C++ 03,5.2.2.7、 "_or class type_"参照)を渡すことができると考えました。そして、 'struct mt'は、(a)非PODメンバーを持っていない、(b)ユーザー定義のコピー代入演算子を持たない、(c)ユーザーがいない、定義されたデストラクタ(9節参照)。この控除は間違っていますか? –

+0

@AndreyVihrov Varargsはハックの何かであり、あなたが見つけたようにポータブルではありません。メソッド間でオブジェクトを渡すには、 'std :: 'やその他のコレクションクラスを使用します。 – trojanfoe

+2

私は同意しません。 Varargsは公式にCとC++の両方の標準の一部であるため、移植性があります:-) –

関連する問題