2012-07-04 13 views
5

¿Por qué sucede? El código funciona y no genera advertencias con GCC 4.7 en Linux y MSVC++ 2010 en Windows. Sin embargo, en ideone.com es crashes con SIGILL. ¿Hay un comportamiento indefinido involucrado aquí?¿Por qué este código de C++ obtiene SIGILL en ideone.com?

#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")); 
} 

Respuesta

1

que tiene errores con GCC 4.4.6:

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) 

No se puede pasar a través struct s varargs parámetros de la función, en lugar de pasar punteros:

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); 
} 

que funciona muy bien en mi sistema :

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

He pensado que uno puede pasar cualquier cosa que sea POD (ver C++ 03, 5.2.2.7, "_o clase type_"). Y 'struct mt' parece POD para mí, porque (a) no tiene miembros que no sean POD, (b) no tiene un operador de asignación de copias definido por el usuario y (c) no tiene un usuario destructor definido (ver Cláusula 9). ¿Esta deducción es incorrecta? –

+0

@AndreyVihrov Los Varargs son algo así como un truco y no son portátiles (como habrás descubierto). Use 'std :: ' o alguna otra clase de colección para pasar objetos entre métodos. – trojanfoe

+2

No estoy de acuerdo. Los Varargs son portátiles, porque oficialmente forman parte de los estándares C y C++ :-) –

Cuestiones relacionadas