Is delete *p an alternative to delete [] p?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
The following code is from the Microsoft Documentation
int (**p) () = new (int (*[7]) ());
delete *p;
I think that delete p
should be used here instead.
Is delete *p
the same as delete p
?
c++ arrays
add a comment |
The following code is from the Microsoft Documentation
int (**p) () = new (int (*[7]) ());
delete *p;
I think that delete p
should be used here instead.
Is delete *p
the same as delete p
?
c++ arrays
4
delete *p
differs fromdelete p
.
– Jarod42
7 hours ago
You're right. They're not the same.
– Cruz Jean
7 hours ago
1
typedef
would make thing clearer.
– Jarod42
7 hours ago
Anywayp[0]
is not initialized. They have typo.
– Jarod42
7 hours ago
delete
what wasnew
ed anddelete
what wasnew
ed. Though in modern code you should avoid most uses ofnew
, it's no longer the preferred way of dynamically creating objects. Seestd::make_unique
andstd::make_shared
instead or use a standard container.
– François Andrieux
7 hours ago
add a comment |
The following code is from the Microsoft Documentation
int (**p) () = new (int (*[7]) ());
delete *p;
I think that delete p
should be used here instead.
Is delete *p
the same as delete p
?
c++ arrays
The following code is from the Microsoft Documentation
int (**p) () = new (int (*[7]) ());
delete *p;
I think that delete p
should be used here instead.
Is delete *p
the same as delete p
?
c++ arrays
c++ arrays
edited 7 hours ago
Guillaume Racicot
16.1k53871
16.1k53871
asked 7 hours ago
xiaokaoyxiaokaoy
7152719
7152719
4
delete *p
differs fromdelete p
.
– Jarod42
7 hours ago
You're right. They're not the same.
– Cruz Jean
7 hours ago
1
typedef
would make thing clearer.
– Jarod42
7 hours ago
Anywayp[0]
is not initialized. They have typo.
– Jarod42
7 hours ago
delete
what wasnew
ed anddelete
what wasnew
ed. Though in modern code you should avoid most uses ofnew
, it's no longer the preferred way of dynamically creating objects. Seestd::make_unique
andstd::make_shared
instead or use a standard container.
– François Andrieux
7 hours ago
add a comment |
4
delete *p
differs fromdelete p
.
– Jarod42
7 hours ago
You're right. They're not the same.
– Cruz Jean
7 hours ago
1
typedef
would make thing clearer.
– Jarod42
7 hours ago
Anywayp[0]
is not initialized. They have typo.
– Jarod42
7 hours ago
delete
what wasnew
ed anddelete
what wasnew
ed. Though in modern code you should avoid most uses ofnew
, it's no longer the preferred way of dynamically creating objects. Seestd::make_unique
andstd::make_shared
instead or use a standard container.
– François Andrieux
7 hours ago
4
4
delete *p
differs from delete p
.– Jarod42
7 hours ago
delete *p
differs from delete p
.– Jarod42
7 hours ago
You're right. They're not the same.
– Cruz Jean
7 hours ago
You're right. They're not the same.
– Cruz Jean
7 hours ago
1
1
typedef
would make thing clearer.– Jarod42
7 hours ago
typedef
would make thing clearer.– Jarod42
7 hours ago
Anyway
p[0]
is not initialized. They have typo.– Jarod42
7 hours ago
Anyway
p[0]
is not initialized. They have typo.– Jarod42
7 hours ago
delete
what was new
ed and delete
what was new
ed. Though in modern code you should avoid most uses of new
, it's no longer the preferred way of dynamically creating objects. See std::make_unique
and std::make_shared
instead or use a standard container.– François Andrieux
7 hours ago
delete
what was new
ed and delete
what was new
ed. Though in modern code you should avoid most uses of new
, it's no longer the preferred way of dynamically creating objects. See std::make_unique
and std::make_shared
instead or use a standard container.– François Andrieux
7 hours ago
add a comment |
2 Answers
2
active
oldest
votes
That code is invalid C++, because only pointers-to-objects can be deleted. *p
has type int (*)()
, which is a function pointer, not a pointer to an object.
Even MSVC itself does not compile it, even in permissive mode:
error C2541: 'delete': cannot delete objects that are not pointers
They should have used delete
instead.
In what context one may need to dynamically allocate pointers to functions?
– Ayxan
6 hours ago
@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.
– Acorn
6 hours ago
@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398
– Acorn
6 hours ago
Even though this is specific to MSVC, g++ gives the following similar error when trying to compile:error: cannot delete expression of type 'int (*)()'
.. seems odd that MS would have missed something that simple in their docs :/
– txtechhelp
5 hours ago
add a comment |
- This code specifically has a different issue as it tries to delete an inner element of an array so it will not compile. It would compile if you wrote delete p and not delete *p.
- You can use simple delete on an object allocated by new if It is an array of primitive types. Bad practice, ugly, yes. But not a mistake.
- The difference between delete and delete is that the former also calls destructors. For primitive types, however, it will call simple delete to free the memory internally anyway.
- Again, even if it works, you should avoid mixing operator types.
delete
calls destructors...
– Acorn
6 hours ago
Mixing operators is undefined behavior.
– Ayxan
6 hours ago
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%2f55524140%2fis-delete-p-an-alternative-to-delete-p%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
That code is invalid C++, because only pointers-to-objects can be deleted. *p
has type int (*)()
, which is a function pointer, not a pointer to an object.
Even MSVC itself does not compile it, even in permissive mode:
error C2541: 'delete': cannot delete objects that are not pointers
They should have used delete
instead.
In what context one may need to dynamically allocate pointers to functions?
– Ayxan
6 hours ago
@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.
– Acorn
6 hours ago
@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398
– Acorn
6 hours ago
Even though this is specific to MSVC, g++ gives the following similar error when trying to compile:error: cannot delete expression of type 'int (*)()'
.. seems odd that MS would have missed something that simple in their docs :/
– txtechhelp
5 hours ago
add a comment |
That code is invalid C++, because only pointers-to-objects can be deleted. *p
has type int (*)()
, which is a function pointer, not a pointer to an object.
Even MSVC itself does not compile it, even in permissive mode:
error C2541: 'delete': cannot delete objects that are not pointers
They should have used delete
instead.
In what context one may need to dynamically allocate pointers to functions?
– Ayxan
6 hours ago
@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.
– Acorn
6 hours ago
@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398
– Acorn
6 hours ago
Even though this is specific to MSVC, g++ gives the following similar error when trying to compile:error: cannot delete expression of type 'int (*)()'
.. seems odd that MS would have missed something that simple in their docs :/
– txtechhelp
5 hours ago
add a comment |
That code is invalid C++, because only pointers-to-objects can be deleted. *p
has type int (*)()
, which is a function pointer, not a pointer to an object.
Even MSVC itself does not compile it, even in permissive mode:
error C2541: 'delete': cannot delete objects that are not pointers
They should have used delete
instead.
That code is invalid C++, because only pointers-to-objects can be deleted. *p
has type int (*)()
, which is a function pointer, not a pointer to an object.
Even MSVC itself does not compile it, even in permissive mode:
error C2541: 'delete': cannot delete objects that are not pointers
They should have used delete
instead.
edited 7 hours ago
answered 7 hours ago
AcornAcorn
6,20111341
6,20111341
In what context one may need to dynamically allocate pointers to functions?
– Ayxan
6 hours ago
@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.
– Acorn
6 hours ago
@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398
– Acorn
6 hours ago
Even though this is specific to MSVC, g++ gives the following similar error when trying to compile:error: cannot delete expression of type 'int (*)()'
.. seems odd that MS would have missed something that simple in their docs :/
– txtechhelp
5 hours ago
add a comment |
In what context one may need to dynamically allocate pointers to functions?
– Ayxan
6 hours ago
@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.
– Acorn
6 hours ago
@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398
– Acorn
6 hours ago
Even though this is specific to MSVC, g++ gives the following similar error when trying to compile:error: cannot delete expression of type 'int (*)()'
.. seems odd that MS would have missed something that simple in their docs :/
– txtechhelp
5 hours ago
In what context one may need to dynamically allocate pointers to functions?
– Ayxan
6 hours ago
In what context one may need to dynamically allocate pointers to functions?
– Ayxan
6 hours ago
@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.
– Acorn
6 hours ago
@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.
– Acorn
6 hours ago
@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398
– Acorn
6 hours ago
@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398
– Acorn
6 hours ago
Even though this is specific to MSVC, g++ gives the following similar error when trying to compile:
error: cannot delete expression of type 'int (*)()'
.. seems odd that MS would have missed something that simple in their docs :/– txtechhelp
5 hours ago
Even though this is specific to MSVC, g++ gives the following similar error when trying to compile:
error: cannot delete expression of type 'int (*)()'
.. seems odd that MS would have missed something that simple in their docs :/– txtechhelp
5 hours ago
add a comment |
- This code specifically has a different issue as it tries to delete an inner element of an array so it will not compile. It would compile if you wrote delete p and not delete *p.
- You can use simple delete on an object allocated by new if It is an array of primitive types. Bad practice, ugly, yes. But not a mistake.
- The difference between delete and delete is that the former also calls destructors. For primitive types, however, it will call simple delete to free the memory internally anyway.
- Again, even if it works, you should avoid mixing operator types.
delete
calls destructors...
– Acorn
6 hours ago
Mixing operators is undefined behavior.
– Ayxan
6 hours ago
add a comment |
- This code specifically has a different issue as it tries to delete an inner element of an array so it will not compile. It would compile if you wrote delete p and not delete *p.
- You can use simple delete on an object allocated by new if It is an array of primitive types. Bad practice, ugly, yes. But not a mistake.
- The difference between delete and delete is that the former also calls destructors. For primitive types, however, it will call simple delete to free the memory internally anyway.
- Again, even if it works, you should avoid mixing operator types.
delete
calls destructors...
– Acorn
6 hours ago
Mixing operators is undefined behavior.
– Ayxan
6 hours ago
add a comment |
- This code specifically has a different issue as it tries to delete an inner element of an array so it will not compile. It would compile if you wrote delete p and not delete *p.
- You can use simple delete on an object allocated by new if It is an array of primitive types. Bad practice, ugly, yes. But not a mistake.
- The difference between delete and delete is that the former also calls destructors. For primitive types, however, it will call simple delete to free the memory internally anyway.
- Again, even if it works, you should avoid mixing operator types.
- This code specifically has a different issue as it tries to delete an inner element of an array so it will not compile. It would compile if you wrote delete p and not delete *p.
- You can use simple delete on an object allocated by new if It is an array of primitive types. Bad practice, ugly, yes. But not a mistake.
- The difference between delete and delete is that the former also calls destructors. For primitive types, however, it will call simple delete to free the memory internally anyway.
- Again, even if it works, you should avoid mixing operator types.
answered 6 hours ago
Yuri NudelmanYuri Nudelman
1,4061614
1,4061614
delete
calls destructors...
– Acorn
6 hours ago
Mixing operators is undefined behavior.
– Ayxan
6 hours ago
add a comment |
delete
calls destructors...
– Acorn
6 hours ago
Mixing operators is undefined behavior.
– Ayxan
6 hours ago
delete
calls destructors...– Acorn
6 hours ago
delete
calls destructors...– Acorn
6 hours ago
Mixing operators is undefined behavior.
– Ayxan
6 hours ago
Mixing operators is undefined behavior.
– Ayxan
6 hours ago
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%2f55524140%2fis-delete-p-an-alternative-to-delete-p%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
4
delete *p
differs fromdelete p
.– Jarod42
7 hours ago
You're right. They're not the same.
– Cruz Jean
7 hours ago
1
typedef
would make thing clearer.– Jarod42
7 hours ago
Anyway
p[0]
is not initialized. They have typo.– Jarod42
7 hours ago
delete
what wasnew
ed anddelete
what wasnew
ed. Though in modern code you should avoid most uses ofnew
, it's no longer the preferred way of dynamically creating objects. Seestd::make_unique
andstd::make_shared
instead or use a standard container.– François Andrieux
7 hours ago