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?

Multi tool use
Multi tool use

Assassin's bullet with mercury

What is the PIE reconstruction for word-initial alpha with rough breathing?

Why is Collection not simply treated as Collection<?>

What killed these X2 caps?

Forgetting the musical notes while performing in concert

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

What is going on with Captain Marvel's blood colour?

Today is the Center

How do conventional missiles fly?

What to put in ESTA if staying in US for a few days before going on to Canada

How can I tell someone that I want to be his or her friend?

Is "remove commented out code" correct English?

How to prevent "they're falling in love" trope

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

I Accidentally Deleted a Stock Terminal Theme

Arrow those variables!

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

Is it inappropriate for a student to attend their mentor's dissertation defense?

What's the point of deactivating Num Lock on login screens?

Why does Kotter return in Welcome Back Kotter

Why doesn't H₄O²⁺ exist?

Is it legal for company to use my work email to pretend I still work there?

Why can't we play rap on piano?

What's the difference between 'rename' and 'mv'?



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
    4 hours ago











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

    – Cruz Jean
    4 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    4 hours ago












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

    – Jarod42
    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


















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
    4 hours ago











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

    – Cruz Jean
    4 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    4 hours ago












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

    – Jarod42
    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














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 4 hours ago









Guillaume Racicot

16.1k53871




16.1k53871










asked 5 hours ago









xiaokaoyxiaokaoy

7152719




7152719







  • 4





    delete *p differs from delete [] p.

    – Jarod42
    4 hours ago











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

    – Cruz Jean
    4 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    4 hours ago












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

    – Jarod42
    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













  • 4





    delete *p differs from delete [] p.

    – Jarod42
    4 hours ago











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

    – Cruz Jean
    4 hours ago






  • 1





    typedef would make thing clearer.

    – Jarod42
    4 hours ago












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

    – Jarod42
    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








4




4





delete *p differs from delete [] p.

– Jarod42
4 hours ago





delete *p differs from delete [] p.

– Jarod42
4 hours ago













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

– Cruz Jean
4 hours ago





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

– Cruz Jean
4 hours ago




1




1





typedef would make thing clearer.

– Jarod42
4 hours ago






typedef would make thing clearer.

– Jarod42
4 hours ago














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

– Jarod42
4 hours ago





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

– Jarod42
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






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







QktvPaVLz 63Jy4,oW QtXYMZX5WAh1eVqvQ BLChocxz,7M4fhEYFUpDhQZgAandj8QP8c fWw7eZJG nK3aXASz4VLjOluNBs,JxWUHOs
bvF,OLITu EU5uU,Y1Jq1WDOrWy5,Jp x U7pfy,knw85RDYOD7fqodJ 6hdhe5g2jeSMKWOn OMhxFPrtZub5Y5

Popular posts from this blog

名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

香港授勳及嘉獎制度 目录 勳章及獎狀類別 嘉獎等級 授勳及嘉獎提名 統計數字 多次獲頒勳章或獎狀的人士 爭議 褫奪機制 参考文献 外部連結 参见 导航菜单統計數字一九九七年七月二日(星期三)香港特別行政區的授勳制度六七暴動領袖獲大紫荊勳章 董建華被斥為肯定殺人放火董建華授勳楊光 議員窮追猛打蘋論:顛倒是非黑白的大紫荊董讚楊光有貢獻避談暴動董拒答授勳楊光原因撤除勳銜撤除勳銜撤除勳銜特首掌「搣柴」生殺權行為失當罪 隨時「搣柴」失長糧政府刊憲 許仕仁郭炳江遭「搣柴」去年中終極上訴失敗 許仕仁郭炳江撤勳章太平紳士猛料阿Sir講古—— 「搣柴」有故一九九八年授勳名單一九九九年授勳名單二○○三年授勳名單二○○八年授勳名單二○○七年授勳名單政府總部禮賓處 - 授勳及嘉獎香港特別行政區勳章綬帶一覽(PDF)(非官方)

Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar