make_shared



  • Aus "Scott Meyers neueste[m] Buch" (fragliche Stelle angestrichen):

    Item 21, Seite 141 schrieb:

    If std::make_shared is used instead,

    auto spw = std::make_shared<Widget>();
    

    one allocation suffices. That’s because std::make_shared allocates a single chunk of memory to hold both the Widget object and the control block. This optimization reduces the static size of the program, because the code contains only one memory allocation call, and it increases the speed of the executable code, because memory is allocated only once. Furthermore, using std::make_shared obviates the need for some of the bookkeeping information in the control block, potentially reducing the total memory footprint for the program.

    Warum schreibt der so einen Quatsch? Erstens ist die Aussage falsch. Wenn ich

    #ifdef A
    auto f() {
      return std::make_shared<int>();
    }
    #pragma message ("make_shared")
    #else
    auto f() {
      return std::shared_ptr<int>(new int);
    }
    #pragma message ("new int")
    #endif
    

    g++ -std=c++14 -O3 kompiliere, erhalte ich 9648 Bytes für make_shared und 9456 Bytes für new int .

    Zweitens ist es völlig unsinnig, eine Bemerkung über die statische Programmgrösse zu machen.

    Er könnte genauso gut lügen, dass make_shared schneller kompiliert, weil der Typ nur einmal vorkommt (in der Tat hat er diese Assoziation schon vorher gemacht:

    Item 21, Seite 140 schrieb:

    Repeating types runs afoul of a key tenet of software engineering: code duplication should be avoided. Duplication in source code increases compilation times, can lead to bloated object code, and generally renders a code base more difficult to work with. It often evolves into inconsistent code, and inconsistency in a code base often leads to bugs.

    )

    Mir kommt es vor, als ziehe er sich die Argumente an den Haaren herbei und schreckt nicht davor zurück, Unwahrheiten zu erzählen.

    Was soll das? Ist das seine vielgelobte Didaktik? Findet ihr das nicht auch völlig unprofessionell?


  • Mod

    meybash schrieb:

    Warum schreibt der so einen Quatsch?

    Was soll das? Ist das seine vielgelobte Didaktik? Findet ihr das nicht auch völlig unprofessionell?

    Warum verschwendest du unsere Zeit mit irrelevanten Fragen?


Anmelden zum Antworten