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 6146879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:01:20+00:00 2026-05-23T19:01:20+00:00

char * buf = new char[sizeof(T)]; new (buf) T; T * t = (T

  • 0
char * buf = new char[sizeof(T)];
new (buf) T;
T * t = (T *)buf;
//code...
//here I should destruct *t but as it is argument of template and can be
//instantiated via basic types as well (say int) so such code 
/*t->~T();*/
//is incorrect (maybe correct? Strange, but it works on VS 2005 for basic types.)
//and this code 
/*delete t;*/ 
//crashes the program.
delete [] buf;

So what is correct way to destruct t?

P.S. The code above is only for describing my problem, and have not real relationship with code I’m going to write. So please don’t give answers like (Why use placement new instead of non-placement? or something similar)

  • 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-23T19:01:20+00:00Added an answer on May 23, 2026 at 7:01 pm

    …instantiated via basic types as well (say int) so such code

    t->~T(); is incorrect
    …

    Wrong. That code is legal and correct in template code even if T can be a primitive type.

    C++ standard: 5.4.2

    5.2.4 Pseudo destructor call [expr.pseudo]

    1. The use of a
      pseudo-destructor-name after a dot . or arrow -> operator represents
      the destructor for the non-class type named by type-name. The result
      shall only be used as the operand for the function call operator (),
      and the result of such a call has type void. The only effect is the
      evaluation of the postfix expression before the dot or arrow.
    2. The left hand side of the dot operator shall be of scalar type. The left
      hand side of the arrow operator shall be of pointer to scalar type.
      This scalar type is the object type. The type designated by the
      pseudo destructor- name shall be the same as the object type.
      Furthermore, the two type-names in a pseudodestructor- name of the
      form ::opt nested-name-specifieropt type-name :: ˜ type-name shall
      designate the same scalar type. The cv-unqualified versions of the
      object type and of the type designated by the pseudo-destructor-name
      shall be the same type.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a Solaris 5.8 machine, I have the following code: [non-working code] char *buf;
You can specify one buffer for your file stream like that: char buf[BUFFER_SIZE]; std::ofstream
I have two variables: char charTime[] = TIME; char buf[] = SOMETHINGELSE; I want
{ char *a, *b; printf(%lx\n,(b-a)); } Usually works, in fact, I can't imagine it
I am new to linux and was making a module in which user can
We can use placement new to create an object in pre-allocated memory. Let's consider
What is the right method to delete all the memory allocated here? const char*
The clue is in the title but basically I've inherited some code which has
I use setbuf in order to redirect stdout to char buffer But I get
Char's are great because they are fixed size and thus make for a faster

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.