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?
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
add a comment |
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
add a comment |
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
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
c++ c++17 new-operator
edited Mar 22 at 20:44
Cody Gray♦
195k35383475
195k35383475
asked Mar 22 at 20:32
thbthb
8,89032457
8,89032457
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.
add a comment |
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.
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.
add a comment |
If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.
add a comment |
If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.
If construction of any object in the buffer throws an exception, the previously constructed objects must be destructed. That requires an available destructor.
edited Mar 22 at 20:35
Sombrero Chicken
24.7k33281
24.7k33281
answered Mar 22 at 20:34
StoryTellerStoryTeller
104k12218281
104k12218281
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 23 at 7:59
P.WP.W
17.6k41556
17.6k41556
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered yesterday
AjayAjay
13.6k84179
13.6k84179
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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