new int <-> new int()



  • Hallo zusamms,
    beim spielen mit dem gcc (3.3) ist mir aufgefallen, dass sich

    new int;
    

    und

    new int();
    

    unterschiedlich verhalten.
    Die erste Version initialisert den Speicherplatz (abgesehen von der Speicheralloziierung durch new) nicht die 2. jedoch wohl.
    (zu erkennen an diesem Beispiel)

    #include<new>
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    
      char* text =  new char[10] ;
      text[0] = 'a';
      text[1] = 'b';
      text[2] = 00;
    
      int * nummer = reinterpret_cast<int *> (text);
    
      cout << nummer <<endl;
      cout << text << endl;
      cout << *nummer << endl;
    
      void * memory = text;
    
      int * testint = new(memory) int;
    
      cout << testint <<endl;
      cout << text << endl;
      cout << *nummer << endl;
    
      int * testint2 = new(memory) int();
    
      cout << testint2 <<endl;
      cout << text << endl;
      cout << *nummer << endl;
    
    }
    

    Ist das ein vorgesehenes Verhalten nach dem ISO Standard?
    Gibt es einen Unterschied zwischen (z.B. initalisierung eigebetter Variablen?)

    new classa;
    

    und

    new classa();
    

    Lieben Gruß,
    J.



  • Ich denke mal, dass bei new int() ein Pseudo-Ctor für int aufgerufen wird, ala

    int i=int(); //initialisiert
    int j; //nicht initialisiert
    

    Bei Klassen macht das keinen Unterschied, da hier immer ein Ctor aufgerufen wird.



  • "int i = int( 100 );" == "int i = 100;"

    MfG



  • Sicher, dass beim ersten nicht noch ein temporäres Objekt erzeugt wird (ausgeschaltete Optimierung)?



  • Michael E. schrieb:

    Sicher, dass beim ersten nicht noch ein temporäres Objekt erzeugt wird (ausgeschaltete Optimierung)?

    Bei eingebauten Datentypen jedenfalls nicht.



  • Ist das ein vorgesehenes Verhalten nach dem ISO Standard?

    Jup. Ersteres bedeutet für POD-Typen, dass keine Initialisierung stattfindet. Zweiteres erzwingt default-initialization (mittlerweile value-initialization), was für POD-Typen zero-initialization gleich kommt.



  • kingruedi schrieb:

    int i=int(); //initialisiert
    int j; //nicht initialisiert
    

    Das war mir neu, erklärt aber einiges.
    Ich kannte bislang nur den Syntax für Konstruktoren, die nicht der standard Konstruktor sind:

    " int i(300); " == " int i = 300; "

    Bei Klassen macht das keinen Unterschied, da hier immer ein Ctor aufgerufen wird.

    Ich habe noch ein wenig weiter gespielt und festgestellt, dass der Ctor (beim gcc) nur dann immer aufgerufen wird, wenn man den Standard Ctor explizit definiert. Tut man das nicht überträgt sich das verhalten (initalisieren, nicht initalisieren) auf alle eingebetten objecte, für die kein neuer Standard Ctor definiert wurde.

    Zum Spaß hier meine etwas hässliche Cut-and-Paste-Testorgie:

    #include<new>
    #include<iostream>
    
    using namespace std;
    
    class classa
    {
      int val;
     public:
      void printit()
      { cout << val << endl;}
    };
    
    class classb
    {
      int val;
     public:
      classb()
      { cout << "constr cb" << endl;}
      void printit()
      { cout << val << endl;}
    };
    
    class classc
    {
      int val;
     public:
      classc():val(1313)
      { cout << "constr cc" << endl;}
      void printit()
      { cout << val << endl;}
    };
    
    class classd
    {
      classa mclassa;
     public:
      void printit()
      { mclassa.printit();}
    };
    
    class classe
    {
      classa mclassa;
     public:
      classe()
      { cout << "constr ce" << endl;}
      void printit()
      { mclassa.printit();}
    };
    
    int main()
    {
    
      char* text =  new char[10] ;
      text[0] = 'a';
      text[1] = 'b';
      text[2] = 00;
    
      int * nummer = reinterpret_cast<int *> (text);
    
      cout << nummer <<endl;
      cout << text << endl;
      cout << *nummer << endl;
    
      void * memory = text;
    
      int * testint = new(memory) int;
    
      cout << testint <<endl;
      cout << text << endl;
      cout << *nummer << endl;
    
      int * testint2 = new(memory) int();
    
      cout << testint2 <<endl;
      cout << *nummer << endl;
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class A" <<endl;
    
      classa * testclassa = new(memory) classa;
      cout << testclassa <<endl;
      testclassa->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class A()" <<endl;
    
      classa * testclassa1 = new(memory) classa();
      cout << testclassa1 <<endl;
      testclassa1->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class B" <<endl;
    
      classb * testclassb = new(memory) classb;
      cout << testclassb <<endl;
      testclassb->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class B()" <<endl;
    
      classb * testclassb1 = new(memory) classb;
      cout << testclassb1 <<endl;
      testclassb1->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class C" <<endl;
    
      classc * testclassc = new(memory) classc;
      cout << testclassc <<endl;
      testclassc->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class C()" <<endl;
    
      classc * testclassc1 = new(memory) classc();
      cout << testclassc1 <<endl;
      testclassc1->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class D" <<endl;
    
      classd * testclassd = new(memory) classd;
      cout << testclassd <<endl;
      testclassd->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class D()" <<endl;
    
      classd * testclassd1 = new(memory) classd();
      cout << testclassd1 <<endl;
      testclassd1->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class E" <<endl;
    
      classe * testclasse = new(memory) classe;
      cout << testclasse <<endl;
      testclasse->printit();
    
      text[0] = 'a';text[1] = 'b';text[2] = 00;
      cout << "test Class E()" <<endl;
    
      classe * testclasse1 = new(memory) classe();
      cout << testclasse1 <<endl;
      testclasse1->printit();
    
    }
    

Anmelden zum Antworten