2012-07-04 16 views
5

ऐसा क्यों होता है? कोड काम करता है और लिनक्स पर जीसीसी 4.7 और विंडोज़ पर एमएसवीसी ++ 2010 के साथ कोई चेतावनी उत्पन्न नहीं करता है। फिर भी, ideone.com पर crashesSIGILL के साथ। क्या अपरिभाषित व्यवहार यहां शामिल है?यह सी ++ कोड 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

मैं जीसीसी 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) 

आप struct रों पारित नहीं हो सकता समारोह मापदंडों varargs के माध्यम से, बजाय पारित संकेत:

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

मैंने सोचा है कि कोई भी पीओडी पास कर सकता है (सी ++ 03, 5.2.2.7, "_or class type_" देखें)। और 'स्ट्रक्चर एमटी' मुझे पीओडी दिखता है, क्योंकि (ए) इसमें गैर-पीओडी सदस्य नहीं हैं, (बी) इसमें उपयोगकर्ता द्वारा परिभाषित कॉपी असाइनमेंट ऑपरेटर नहीं है और (सी) इसमें उपयोगकर्ता नहीं है परिभाषित विनाशक (धारा 9 देखें)। क्या यह कटौती गलत है? –

+0

@AndreyVihrov Varargs एक हैक के कुछ हैं और गैर पोर्टेबल हैं (जैसा कि आपने पाया है)। विधियों के बीच वस्तुओं को पारित करने के लिए 'std :: ' या कुछ अन्य संग्रह वर्ग का उपयोग करें। – trojanfoe

+2

मैं असहमत हूं। Varargs पोर्टेबल हैं, क्योंकि वे आधिकारिक तौर पर सी और सी ++ मानकों का हिस्सा हैं :-) –

संबंधित मुद्दे