Why would a new[] expression ever invoke a destructor? The Next CEO of Stack OverflowAbout constructors/destructors and new/delete operators in C++ for custom objectsDoes delete call the destructor?Overloading operator delete in a base classWhen is %destructor invoked in BISON?Why should C++ programmers minimize use of 'new'?Why is ::operator new[] necessary when ::operator new is enough?Why am I permitted to declare an object with a deleted destructor?Does delete[] deallocate memory in one shot after invoking destructors?Why are non-placement `new` and `delete` built into the language and not just regular functions?Why deallocation function is not called when object constructor throw in a new expression?

Science fiction short story involving a paper written by a schizophrenic

How did people program for Consoles with multiple CPUs?

Extracting names from filename in bash

Increase performance creating Mandelbrot set in python

A "random" question: usage of "random" as adjective in Spanish

Clustering points and summing up attributes per cluster in QGIS

If/When UK leaves the EU, can a future goverment do a referendum to join EU

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

Oh, one short ode of love

What can we do to stop prior company from asking us questions?

Why doesn't a table tennis ball float on a surface of steel balls? How do we calculate buoyancy here?

Apart from "berlinern", do any other German dialects have a corresponding verb?

Unreliable Magic - Is it worth it?

How to start emacs in "nothing" mode (`fundamental-mode`)

Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?

Monthly twice production release for my software project

Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?

How to make a software documentation "officially" citable?

Title page not generated

How to get regions to plot as graphics

How do I construct this japanese bowl?

What flight has the highest ratio of time difference to flight time?

On model categories where every object is bifibrant

What is the difference between Sanyaas and Vairagya?



Why would a new[] expression ever invoke a destructor?



The Next CEO of Stack OverflowAbout constructors/destructors and new/delete operators in C++ for custom objectsDoes delete call the destructor?Overloading operator delete in a base classWhen is %destructor invoked in BISON?Why should C++ programmers minimize use of 'new'?Why is ::operator new[] necessary when ::operator new is enough?Why am I permitted to declare an object with a deleted destructor?Does delete[] deallocate memory in one shot after invoking destructors?Why are non-placement `new` and `delete` built into the language and not just regular functions?Why deallocation function is not called when object constructor throw in a new expression?










34















From the C++17 standard (draft here), [expr.new]:




If the new-expression creates an object or an array of objects of class type, access and ambiguity control are done for the allocation function, the deallocation function, and the constructor. If the new-expression creates an array of objects of class type, the destructor is potentially invoked.




Why would new[] invoke a destructor? It's new, after all. It isn't delete.










share|improve this question




























    34















    From the C++17 standard (draft here), [expr.new]:




    If the new-expression creates an object or an array of objects of class type, access and ambiguity control are done for the allocation function, the deallocation function, and the constructor. If the new-expression creates an array of objects of class type, the destructor is potentially invoked.




    Why would new[] invoke a destructor? It's new, after all. It isn't delete.










    share|improve this question


























      34












      34








      34


      1






      From the C++17 standard (draft here), [expr.new]:




      If the new-expression creates an object or an array of objects of class type, access and ambiguity control are done for the allocation function, the deallocation function, and the constructor. If the new-expression creates an array of objects of class type, the destructor is potentially invoked.




      Why would new[] invoke a destructor? It's new, after all. It isn't delete.










      share|improve this question
















      From the C++17 standard (draft here), [expr.new]:




      If the new-expression creates an object or an array of objects of class type, access and ambiguity control are done for the allocation function, the deallocation function, and the constructor. If the new-expression creates an array of objects of class type, the destructor is potentially invoked.




      Why would new[] invoke a destructor? It's new, after all. It isn't delete.







      c++ c++17 new-operator






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 20:44









      Cody Gray

      195k35383475




      195k35383475










      asked Mar 22 at 20:32









      thbthb

      8,89032457




      8,89032457






















          3 Answers
          3






          active

          oldest

          votes


















          52














          If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.






          share|improve this answer
































            11














            You have not considered the word "potentially" in the quote you have mentioned from the standard.

            It means that there is a possibility that invocation of the destructor can happen. And it will happen if construction of any object in the array throws an exception.



            Combined with the following quote from [class.dtor]/12.4 which mentions [expr.new], this becomes clear.




            In each case, the context of the invocation is the context of the construction of the object. A destructor is also invoked implicitly through use of a delete-expression for a constructed object allocated by a new-expression; the context of the invocation is the delete-expression. [ Note: An array of class type contains several subobjects for each of which the destructor is invoked.  — end note ] A destructor can also be invoked explicitly. A destructor is potentially invoked if it is invoked or as specified in [expr.new], [class.base.init], and [except.throw]. A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.







            share|improve this answer






























              6














              In action:



              #include <iostream>

              int counter;

              class Destruct

              public:
              Destruct()

              if (counter++ > 5)
              throw counter;


              ~Destruct()

              std::cout << "Dtor calledn";

              ;

              int main()

              try

              new Destruct[10];

              catch (...)



              You'll see output something like:



              Dtor called
              Dtor called
              Dtor called
              Dtor called
              Dtor called
              Dtor called





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307381%2fwhy-would-a-new-expression-ever-invoke-a-destructor%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                52














                If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.






                share|improve this answer





























                  52














                  If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.






                  share|improve this answer



























                    52












                    52








                    52







                    If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.






                    share|improve this answer















                    If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 22 at 20:35









                    Sombrero Chicken

                    24.7k33281




                    24.7k33281










                    answered Mar 22 at 20:34









                    StoryTellerStoryTeller

                    104k12218281




                    104k12218281























                        11














                        You have not considered the word "potentially" in the quote you have mentioned from the standard.

                        It means that there is a possibility that invocation of the destructor can happen. And it will happen if construction of any object in the array throws an exception.



                        Combined with the following quote from [class.dtor]/12.4 which mentions [expr.new], this becomes clear.




                        In each case, the context of the invocation is the context of the construction of the object. A destructor is also invoked implicitly through use of a delete-expression for a constructed object allocated by a new-expression; the context of the invocation is the delete-expression. [ Note: An array of class type contains several subobjects for each of which the destructor is invoked.  — end note ] A destructor can also be invoked explicitly. A destructor is potentially invoked if it is invoked or as specified in [expr.new], [class.base.init], and [except.throw]. A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.







                        share|improve this answer



























                          11














                          You have not considered the word "potentially" in the quote you have mentioned from the standard.

                          It means that there is a possibility that invocation of the destructor can happen. And it will happen if construction of any object in the array throws an exception.



                          Combined with the following quote from [class.dtor]/12.4 which mentions [expr.new], this becomes clear.




                          In each case, the context of the invocation is the context of the construction of the object. A destructor is also invoked implicitly through use of a delete-expression for a constructed object allocated by a new-expression; the context of the invocation is the delete-expression. [ Note: An array of class type contains several subobjects for each of which the destructor is invoked.  — end note ] A destructor can also be invoked explicitly. A destructor is potentially invoked if it is invoked or as specified in [expr.new], [class.base.init], and [except.throw]. A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.







                          share|improve this answer

























                            11












                            11








                            11







                            You have not considered the word "potentially" in the quote you have mentioned from the standard.

                            It means that there is a possibility that invocation of the destructor can happen. And it will happen if construction of any object in the array throws an exception.



                            Combined with the following quote from [class.dtor]/12.4 which mentions [expr.new], this becomes clear.




                            In each case, the context of the invocation is the context of the construction of the object. A destructor is also invoked implicitly through use of a delete-expression for a constructed object allocated by a new-expression; the context of the invocation is the delete-expression. [ Note: An array of class type contains several subobjects for each of which the destructor is invoked.  — end note ] A destructor can also be invoked explicitly. A destructor is potentially invoked if it is invoked or as specified in [expr.new], [class.base.init], and [except.throw]. A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.







                            share|improve this answer













                            You have not considered the word "potentially" in the quote you have mentioned from the standard.

                            It means that there is a possibility that invocation of the destructor can happen. And it will happen if construction of any object in the array throws an exception.



                            Combined with the following quote from [class.dtor]/12.4 which mentions [expr.new], this becomes clear.




                            In each case, the context of the invocation is the context of the construction of the object. A destructor is also invoked implicitly through use of a delete-expression for a constructed object allocated by a new-expression; the context of the invocation is the delete-expression. [ Note: An array of class type contains several subobjects for each of which the destructor is invoked.  — end note ] A destructor can also be invoked explicitly. A destructor is potentially invoked if it is invoked or as specified in [expr.new], [class.base.init], and [except.throw]. A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.








                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 23 at 7:59









                            P.WP.W

                            17.6k41556




                            17.6k41556





















                                6














                                In action:



                                #include <iostream>

                                int counter;

                                class Destruct

                                public:
                                Destruct()

                                if (counter++ > 5)
                                throw counter;


                                ~Destruct()

                                std::cout << "Dtor calledn";

                                ;

                                int main()

                                try

                                new Destruct[10];

                                catch (...)



                                You'll see output something like:



                                Dtor called
                                Dtor called
                                Dtor called
                                Dtor called
                                Dtor called
                                Dtor called





                                share|improve this answer



























                                  6














                                  In action:



                                  #include <iostream>

                                  int counter;

                                  class Destruct

                                  public:
                                  Destruct()

                                  if (counter++ > 5)
                                  throw counter;


                                  ~Destruct()

                                  std::cout << "Dtor calledn";

                                  ;

                                  int main()

                                  try

                                  new Destruct[10];

                                  catch (...)



                                  You'll see output something like:



                                  Dtor called
                                  Dtor called
                                  Dtor called
                                  Dtor called
                                  Dtor called
                                  Dtor called





                                  share|improve this answer

























                                    6












                                    6








                                    6







                                    In action:



                                    #include <iostream>

                                    int counter;

                                    class Destruct

                                    public:
                                    Destruct()

                                    if (counter++ > 5)
                                    throw counter;


                                    ~Destruct()

                                    std::cout << "Dtor calledn";

                                    ;

                                    int main()

                                    try

                                    new Destruct[10];

                                    catch (...)



                                    You'll see output something like:



                                    Dtor called
                                    Dtor called
                                    Dtor called
                                    Dtor called
                                    Dtor called
                                    Dtor called





                                    share|improve this answer













                                    In action:



                                    #include <iostream>

                                    int counter;

                                    class Destruct

                                    public:
                                    Destruct()

                                    if (counter++ > 5)
                                    throw counter;


                                    ~Destruct()

                                    std::cout << "Dtor calledn";

                                    ;

                                    int main()

                                    try

                                    new Destruct[10];

                                    catch (...)



                                    You'll see output something like:



                                    Dtor called
                                    Dtor called
                                    Dtor called
                                    Dtor called
                                    Dtor called
                                    Dtor called






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered yesterday









                                    AjayAjay

                                    13.6k84179




                                    13.6k84179



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307381%2fwhy-would-a-new-expression-ever-invoke-a-destructor%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                                        Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

                                        Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High