Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 591045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:35:37+00:00 2026-05-13T15:35:37+00:00

In my code I have effectively the following: wchar_t* buffer = new wchar_t[size]; //

  • 0

In my code I have effectively the following:

wchar_t* buffer = new wchar_t[size];
// bonus irrelevant code here
delete[] reinterpret_cast<char*>( buffer );

Types in question are all built-in and so they have trivial destructors. In VC++ the code above works allright – new[] just allocates memory, then delete[] just frees it.

Is it acceptable in C++? Is it undefined behaviour?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:35:37+00:00Added an answer on May 13, 2026 at 3:35 pm

    My initial thought was that it is undefined behavior.

    5.3.5/3: “In the second alternative (delete array) if the dynamic
    type of the object to be deleted
    differs from its static type, the
    behavior is undefined.73).

    Footnote 73 reads, “This implies that an object cannot be deleted using a pointer of type void* because there are no objects of type void“.

    Arguably the object in your example doesn’t have a dynamic type, since the definition of “dynamic type” at 1.3.3 mentions “most derived object”, and the definition of “most derived object” at 1.8/4 is talking about objects of class type. So I kept looking:

    5.2.10/3: “[reinterpret_cast] might, or might not, produce a representation
    different from the original value”

    5.3.5/2: “The value of the operand of delete shall be the pointer value
    which resulted from a previous array
    new-expression“.

    I’m not sure whether a reinterpret_cast results in the same pointer value as was input, or not. Possibly it’s cleared up by some other bit of the standard which I haven’t found yet. I would not call this code “OK” without finding something to definitively state that if you reinterpret_cast a pointer, the result is the same “pointer value” as before, so that by passing it to delete[] you are passing “the pointer value” from new[].

    5.2.10/7: “Except that casting [between certain pointer types] and
    back to its original type yields the
    original pointer value, the result of
    such a pointer conversion is
    unspecified”.

    This looks like bad news to me – it conspicuously doesn’t say that the cast yields the same value, only that the pair of casts over and back, yields the same value. This suggests to me that the single cast is allowed to yield a different value, but it is only suggestive, not explicit. This is the usual problem with the rule that “if the standard doesn’t state the behavior, then the behavior is undefined”. Just because it doesn’t state it in any of the paragraphs I can find using the index, doesn’t mean it doesn’t state it somewhere else…

    We know that in practice we can cast things to unsigned char* in order to inspect their bytes, or void* to copy PODs using memcpy, so there must be some casts guaranteed to create aliases. You might think that if your implementation does create aliases with certain casts, then you’re passing in the “same value” you got from new[]. But I’m still not sure that’s good enough for delete[]. I think I’m missing something important.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I found a piece of C++ code that effectively does the following: char*
I have code similar to the following: <div class=container> <div class=overlay> // hover content
I have the following code: (query added) $weight_table = mysql_query (SELECT * FROM .$prefix.weight
I have the following code to connect to my office 365 account using powershell:
I have the following code to display a clients age. <TextBox x:Name=txtClientAge Text={Binding Path=ClientAge}
I've followed the guide Setting Up Socket Streams and have effectively duplicated that code
I have the following code: private bool IsMousetrapFile(string path) { logger.Log(validateFileMessage + path); Excel.Application
We have some code on production which is effectively doing what this code does:
I have the following code behind that was, until recently, working fine and dandy!
I have the following code to run python and get the result in scratch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.