Is delete *p an alternative to delete [] p?In what context one may need to dynamically allocate pointers to functions?What are the differences between a pointer variable and a reference variable in C++?PHP: Delete an element from an arrayDeleting array elements in JavaScript - delete vs spliceWhy is “using namespace std” considered bad practice?What is the “-->” operator in C++?What is the copy-and-swap idiom?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is it faster to process a sorted array than an unsorted array?Why should I use a pointer rather than the object itself?

Could gravitational lensing be used to protect a spaceship from a laser?

Python: return float 1.0 as int 1 but float 1.5 as float 1.5

Can I ask the recruiters in my resume to put the reason why I am rejected?

How to take photos in burst mode, without vibration?

How can I make my BBEG immortal short of making them a Lich or Vampire?

Can a rocket refuel on Mars from water?

Blender 2.8 I can't see vertices, edges or faces in edit mode

UK: Is there precedent for the governments e-petition site changing the direction of a government decision?

Doing something right before you need it - expression for this?

How do I write bicross product symbols in latex?

In Romance of the Three Kingdoms why do people still use bamboo sticks when papers are already invented?

Emailing HOD to enhance faculty application

What mechanic is there to disable a threat instead of killing it?

Watching something be written to a file live with tail

Is it possible to download Internet Explorer on my Mac running OS X El Capitan?

A reference to a well-known characterization of scattered compact spaces

How to show the equivalence between the regularized regression and their constraint formulas using KKT

SSH "lag" in LAN on some machines, mixed distros

prove that the matrix A is diagonalizable

Is it canonical bit space?

Is the Joker left-handed?

How badly should I try to prevent a user from XSSing themselves?

I'm flying to France today and my passport expires in less than 2 months

Will google still index a page if I use a $_SESSION variable?



Is delete *p an alternative to delete [] p?


In what context one may need to dynamically allocate pointers to functions?What are the differences between a pointer variable and a reference variable in C++?PHP: Delete an element from an arrayDeleting array elements in JavaScript - delete vs spliceWhy is “using namespace std” considered bad practice?What is the “-->” operator in C++?What is the copy-and-swap idiom?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is it faster to process a sorted array than an unsorted array?Why should I use a pointer rather than the object itself?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








11















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?










share|improve this question



















  • 4





    delete *p differs from delete [] p.

    – Jarod42
    5 hours ago











  • You're right. They're not the same.

    – Cruz Jean
    5 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    5 hours ago












  • Anyway p[0] is not initialized. They have typo.

    – Jarod42
    5 hours ago











  • delete[] what was new[]ed and delete what was newed. 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
    4 hours ago


















11















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?










share|improve this question



















  • 4





    delete *p differs from delete [] p.

    – Jarod42
    5 hours ago











  • You're right. They're not the same.

    – Cruz Jean
    5 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    5 hours ago












  • Anyway p[0] is not initialized. They have typo.

    – Jarod42
    5 hours ago











  • delete[] what was new[]ed and delete what was newed. 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
    4 hours ago














11












11








11








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 5 hours ago









Guillaume Racicot

16.1k53871




16.1k53871










asked 5 hours ago









xiaokaoyxiaokaoy

7152719




7152719







  • 4





    delete *p differs from delete [] p.

    – Jarod42
    5 hours ago











  • You're right. They're not the same.

    – Cruz Jean
    5 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    5 hours ago












  • Anyway p[0] is not initialized. They have typo.

    – Jarod42
    5 hours ago











  • delete[] what was new[]ed and delete what was newed. 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
    4 hours ago













  • 4





    delete *p differs from delete [] p.

    – Jarod42
    5 hours ago











  • You're right. They're not the same.

    – Cruz Jean
    5 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    5 hours ago












  • Anyway p[0] is not initialized. They have typo.

    – Jarod42
    5 hours ago











  • delete[] what was new[]ed and delete what was newed. 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
    4 hours ago








4




4





delete *p differs from delete [] p.

– Jarod42
5 hours ago





delete *p differs from delete [] p.

– Jarod42
5 hours ago













You're right. They're not the same.

– Cruz Jean
5 hours ago





You're right. They're not the same.

– Cruz Jean
5 hours ago




1




1





typedef would make thing clearer.

– Jarod42
5 hours ago






typedef would make thing clearer.

– Jarod42
5 hours ago














Anyway p[0] is not initialized. They have typo.

– Jarod42
5 hours ago





Anyway p[0] is not initialized. They have typo.

– Jarod42
5 hours ago













delete[] what was new[]ed and delete what was newed. 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
4 hours ago






delete[] what was new[]ed and delete what was newed. 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
4 hours ago













2 Answers
2






active

oldest

votes


















12














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.






share|improve this answer

























  • In what context one may need to dynamically allocate pointers to functions?

    – Ayxan
    4 hours ago











  • @Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.

    – Acorn
    4 hours ago











  • @Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398

    – Acorn
    4 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
    3 hours ago



















-3














  1. 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.

  2. 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.

  3. 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.

  4. Again, even if it works, you should avoid mixing operator types.





share|improve this answer























  • delete [] calls destructors...

    – Acorn
    4 hours ago











  • Mixing operators is undefined behavior.

    – Ayxan
    4 hours ago











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%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









12














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.






share|improve this answer

























  • In what context one may need to dynamically allocate pointers to functions?

    – Ayxan
    4 hours ago











  • @Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.

    – Acorn
    4 hours ago











  • @Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398

    – Acorn
    4 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
    3 hours ago
















12














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.






share|improve this answer

























  • In what context one may need to dynamically allocate pointers to functions?

    – Ayxan
    4 hours ago











  • @Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.

    – Acorn
    4 hours ago











  • @Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398

    – Acorn
    4 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
    3 hours ago














12












12








12







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









AcornAcorn

6,20111341




6,20111341












  • In what context one may need to dynamically allocate pointers to functions?

    – Ayxan
    4 hours ago











  • @Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.

    – Acorn
    4 hours ago











  • @Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398

    – Acorn
    4 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
    3 hours ago


















  • In what context one may need to dynamically allocate pointers to functions?

    – Ayxan
    4 hours ago











  • @Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.

    – Acorn
    4 hours ago











  • @Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398

    – Acorn
    4 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
    3 hours ago

















In what context one may need to dynamically allocate pointers to functions?

– Ayxan
4 hours ago





In what context one may need to dynamically allocate pointers to functions?

– Ayxan
4 hours ago













@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.

– Acorn
4 hours ago





@Ayxan That would be another question, but for instance you may want to have a list of arbitrary operations to execute.

– Acorn
4 hours ago













@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398

– Acorn
4 hours ago





@Ayxan I have quickly posted it here: stackoverflow.com/q/55524710/9305398

– Acorn
4 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
3 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
3 hours ago














-3














  1. 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.

  2. 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.

  3. 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.

  4. Again, even if it works, you should avoid mixing operator types.





share|improve this answer























  • delete [] calls destructors...

    – Acorn
    4 hours ago











  • Mixing operators is undefined behavior.

    – Ayxan
    4 hours ago















-3














  1. 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.

  2. 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.

  3. 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.

  4. Again, even if it works, you should avoid mixing operator types.





share|improve this answer























  • delete [] calls destructors...

    – Acorn
    4 hours ago











  • Mixing operators is undefined behavior.

    – Ayxan
    4 hours ago













-3












-3








-3







  1. 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.

  2. 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.

  3. 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.

  4. Again, even if it works, you should avoid mixing operator types.





share|improve this answer













  1. 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.

  2. 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.

  3. 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.

  4. Again, even if it works, you should avoid mixing operator types.






share|improve this answer












share|improve this answer



share|improve this answer










answered 4 hours ago









Yuri NudelmanYuri Nudelman

1,4081614




1,4081614












  • delete [] calls destructors...

    – Acorn
    4 hours ago











  • Mixing operators is undefined behavior.

    – Ayxan
    4 hours ago

















  • delete [] calls destructors...

    – Acorn
    4 hours ago











  • Mixing operators is undefined behavior.

    – Ayxan
    4 hours ago
















delete [] calls destructors...

– Acorn
4 hours ago





delete [] calls destructors...

– Acorn
4 hours ago













Mixing operators is undefined behavior.

– Ayxan
4 hours ago





Mixing operators is undefined behavior.

– Ayxan
4 hours ago

















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%2f55524140%2fis-delete-p-an-alternative-to-delete-p%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

Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?