Is it *legal* (as opposed to sensible) to explicitly destruct anobject and deallocate the memory?

S

Stuart Golodetz

Hi all,

Just feeling curious -- I know the following is ill-advised, but is it
actually formally illegal?

#include <iostream>

struct X
{
~X()
{
std::cout << "~X()\n";
}
};

int main()
{
X *x = new X;
//delete x;
x->~X();
::eek:perator delete(x);
return 0;
}

It would certainly be legal in the context of *placement* new, but is
there a requirement in the standard that all "new"s are matched by
"delete"s, rather than "operator delete"s?

Cheers,
Stu
 
Ö

Öö Tiib

Hi all,

Just feeling curious -- I know the following is ill-advised, but is it
actually formally illegal?

#include <iostream>

struct X
{
        ~X()
        {
                std::cout << "~X()\n";
        }

};

int main()
{
        X *x = new X;
        //delete x;
        x->~X();
        ::eek:perator delete(x);
        return 0;

}

It would certainly be legal in the context of *placement* new, but is
there a requirement in the standard that all "new"s are matched by
"delete"s, rather than "operator delete"s?

It feels legal. It is doing all same things in same order what simple
'delete x;' is doing by standard.
 
S

Stuart Golodetz

It feels legal. It is doing all same things in same order what simple
'delete x;' is doing by standard.

Thanks -- I think I'd expect it to work, just curious because it's the
sort of thing that might be formally undefined for some reason that I
haven't come across.

Stu
 
S

Stuart Golodetz

<snip -- sorry, hit the quoted lines limit>

The following might be a consideration:

struct foo
{
foo()
{
std::cout << "foo ctor called\n";
}
~foo()
{
std::cout << "foo dtor called\n";
}
void* operator new(std::size_t aSize)
{
std::cout << "overloaded operator new called\n";
return ::eek:perator new(aSize);
}
void operator delete(void* p)
{
std::cout << "overloaded operator delete called\n";
return ::eek:perator delete(p);
}
};

int main()
{
foo *x = new foo;
delete x;
//x->~foo();
//::eek:perator delete(x);

foo *y = new foo;
//delete y;
y->~foo();
::eek:perator delete(y);

return 0;
}

outputs:

overloaded operator new called
foo ctor called
foo dtor called
overloaded operator delete called
overloaded operator new called
foo ctor called
foo dtor called

/Leigh

That certainly seems to add one more reason why it's not a sensible
thing to do, at any rate :)

Cheers!
Stu
 
A

Armel

Stuart Golodetz said:
Hi all,

Just feeling curious -- I know the following is ill-advised, but is it
actually formally illegal?

#include <iostream>

struct X
{
~X()
{
std::cout << "~X()\n";
}
};

int main()
{
X *x = new X;
//delete x;
x->~X();
::eek:perator delete(x);
return 0;
}

It would certainly be legal in the context of *placement* new, but is
there a requirement in the standard that all "new"s are matched by
"delete"s, rather than "operator delete"s?

Cheers,
Stu

destructing "by hand" that way is particularly useful when
constructing/destructing objects cannot use standard new/delete operators,
for example to implement classes like the "any" concept and using some
memory place over time to store different objects of classes A then B then
C.
in these constructions the 'placement' new is as well generally used (i.e.
new (x_ptr) X; )

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top