By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

delete[] of POD

P: n/a
int *p;
p = new int[10];

void *q = (void*)p;
delete[] q;

Is this valid if p is guaranteed to be a POD? (don't ask why... there is a
reason I need this. Clearly I will create p in function, use p in another
function that know what type p is and delete p in a function that for
simplicity won't know what p is)

Reading the MSDN it seems that

"The global operator delete function, if declared, takes a single argument
of type void *, which contains a pointer to the object to deallocate. The
return type is void (operator delete cannot return a value). Two forms exist
for class-member operator delete functions:"

so it should be ok But this is the MSDN guide :-)

--- bye
Oct 25 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
MaxMax wrote:
int *p;
p = new int[10];

void *q = (void*)p;
delete[] q;

Is this valid if p is guaranteed to be a POD? (don't ask why... there is a
reason I need this. Clearly I will create p in function, use p in another
function that know what type p is and delete p in a function that for
simplicity won't know what p is)

Reading the MSDN it seems that

"The global operator delete function, if declared, takes a single argument
of type void *, which contains a pointer to the object to deallocate. The
return type is void (operator delete cannot return a value). Two forms exist
for class-member operator delete functions:"

so it should be ok But this is the MSDN guide :-)

--- bye


operator delete[] has a void* as parameter in the standard, so you're fine.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 25 '05 #2

P: n/a
* Ferdi Smit:
MaxMax wrote:
int *p;
p = new int[10];

void *q = (void*)p;
delete[] q;

Is this valid if p is guaranteed to be a POD? (don't ask why... there is a
reason I need this. Clearly I will create p in function, use p in another
function that know what type p is and delete p in a function that for
simplicity won't know what p is)

Reading the MSDN it seems that

"The global operator delete function, if declared, takes a single argument
of type void *, which contains a pointer to the object to deallocate. The
return type is void (operator delete cannot return a value). Two forms exist
for class-member operator delete functions:"

so it should be ok But this is the MSDN guide :-)


operator delete[] has a void* as parameter in the standard, so you're fine.


Sorry, that's an incorrect inference.

The standard defines this as Undefined Behavior, and in case somebody should
not see that implication, it states that explicitly for void* in note 73:

[context: delete array] "... an object cannot be deleted using a pointer of
type void* because ...".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 25 '05 #3

P: n/a
Alf P. Steinbach wrote:

operator delete[] has a void* as parameter in the standard, so you're fine.

Sorry, that's an incorrect inference.

The standard defines this as Undefined Behavior, and in case somebody should
not see that implication, it states that explicitly for void* in note 73:

[context: delete array] "... an object cannot be deleted using a pointer of
type void* because ...".


I think that refers to an array of void which is impossible (size 0, a
similar reason to why an (empty) instantiated object has size at least
1); and thus you can't delete[] a true void*. But in max's case the
underlying data is in fact of another type (int*). The operator delete[]
takes a void*, so whatever pointer you pass it, it will be cast to a
void* first before the operator runs anyway.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 25 '05 #4

P: n/a
* Ferdi Smit:
Alf P. Steinbach wrote:

operator delete[] has a void* as parameter in the standard, so you're fine.

Sorry, that's an incorrect inference.

The standard defines this as Undefined Behavior, and in case somebody should
not see that implication, it states that explicitly for void* in note 73:

[context: delete array] "... an object cannot be deleted using a pointer of
type void* because ...".


I think that refers to an array of void which is impossible (size 0, a
similar reason to why an (empty) instantiated object has size at least
1); and thus you can't delete[] a true void*.


There is no such thing as a "true void*" ;-) Except the nullpointer of void*
type, in which case you have no object to be deleted.

But in max's case the
underlying data is in fact of another type (int*). The operator delete[]
takes a void*, so whatever pointer you pass it, it will be cast to a
void* first before the operator runs anyway.


That doesn't matter. The compiler can't infer the size or type of the array
elements statically. The standard couldn't be more clear on this issue: it's
Undefined Behavior.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 25 '05 #5

P: n/a
"Ferdi Smit" <Fe********@cwi.nl> wrote in message news:Io********@cwi.nl

I think that refers to an array of void which is impossible (size 0, a
similar reason to why an (empty) instantiated object has size at least
1); and thus you can't delete[] a true void*. But in max's case the
underlying data is in fact of another type (int*). The operator
delete[] takes a void*, so whatever pointer you pass it, it will be
cast to a void* first before the operator runs anyway.


You need to distinguish between delete[] and operator delete[], which are
easily confused.

delete[] does two things:

1. it calls the destructor for the objects in the array, and then
2. it calls operator delete[] to deallocate memory.

Once the destructor has run, the object no longer exists and hence it is not
surprising that operator delete[] is given a void* pointer. To see the
effect of using delete[] with a void* pointer, run the following:

#include <iostream>

class Test
{
public:
~Test()
{
std::cout << "Destructor called\n";
}
};

int main()
{
Test *ptest = new Test[4];
void * ptr = (void*)ptest;
delete [] ptr;
return 0;
}

The result of this is undefined, but I predict with reasonable confidence
that the destructor will not be called. If, by contrast, you call delete[]
on ptest, then the destructor will be called.

Of course, if your array members don't have destructors, then you may get
away with calling delete[] on a void* pointer. There is no guarantee,
however. The behaviour is undefined.

--
John Carson

Oct 25 '05 #6

P: n/a
John Carson wrote:
You need to distinguish between delete[] and operator delete[], which are
easily confused.

delete[] does two things:

1. it calls the destructor for the objects in the array, and then
2. it calls operator delete[] to deallocate memory.

Once the destructor has run, the object no longer exists and hence it is
not
surprising that operator delete[] is given a void* pointer. To see the
effect of using delete[] with a void* pointer, run the following:

#include <iostream>

class Test
{
public:
~Test()
{
std::cout << "Destructor called\n";
}
};

int main()
{
Test *ptest = new Test[4];
void * ptr = (void*)ptest;
delete [] ptr;
return 0;
}

The result of this is undefined, but I predict with reasonable confidence
that the destructor will not be called. If, by contrast, you call delete[]
on ptest, then the destructor will be called.

Of course, if your array members don't have destructors, then you may get
away with calling delete[] on a void* pointer. There is no guarantee,
however. The behaviour is undefined.


Ah, alright. Then this time I stand corrected :) (arrays are evil anyway :P)

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Oct 25 '05 #7

P: n/a
>> Of course, if your array members don't have destructors, then you may get
away with calling delete[] on a void* pointer. There is no guarantee,
however. The behaviour is undefined.

So, if I know I'm working with a POD, can I directly use the operator
delete? POD don't have destructors.

--- bye
Oct 29 '05 #8

P: n/a
"MaxMax" <no**@none.com> wrote in message
news:n2********************@twister2.libero.it...
: >> Of course, if your array members don't have destructors, then you
may get
: >> away with calling delete[] on a void* pointer. There is no
guarantee,
: >> however. The behaviour is undefined.
: So, if I know I'm working with a POD, can I directly use the
operator
: delete? POD don't have destructors.

Not if you want your code to be portable and standards-compliant.

Let me restate what was written above about this being undefined
behavior:
you can do it for a POD, and it is even likely to work as you expect
on
your platform/compiler. However, code that uses this trick will not
be portable, and will behave unexpectedly on some other platform,
or when you port your code to a new version of your compiler.

In other words: don't do it.
Allocated memory always must be disposed in a way that strictly
matches the allocation method.

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact
form
Brainbench MVP for C++ <> http://www.brainbench.com
Oct 29 '05 #9

P: n/a
"Ivan Vecerina" <IN*****************@ivan.vecerina.com> wrote in message
news:dj**********@news.hispeed.ch...
"MaxMax" <no**@none.com> wrote in message
news:n2********************@twister2.libero.it...
: >> Of course, if your array members don't have destructors, then you
may get
: >> away with calling delete[] on a void* pointer. There is no
guarantee,
: >> however. The behaviour is undefined.
: So, if I know I'm working with a POD, can I directly use the
operator
: delete? POD don't have destructors.

Not if you want your code to be portable and standards-compliant.

Let me restate what was written above about this being undefined
behavior:
you can do it for a POD, and it is even likely to work as you expect
on
your platform/compiler. However, code that uses this trick will not
be portable, and will behave unexpectedly on some other platform,
or when you port your code to a new version of your compiler.

In other words: don't do it.
Allocated memory always must be disposed in a way that strictly
matches the allocation method.


Seeing how this is POD, couldn't you just call
delete q;
as the destructors don't need to be called?

OP question:
int *p;
p = new int[10];

void *q = (void*)p;
delete[] q;

Is this valid if p is guaranteed to be a POD? ...
Oct 31 '05 #10

P: n/a
Jim Langston wrote:
Seeing how this is POD, couldn't you just call
delete q;
as the destructors don't need to be called?
The standard makes no such exception for POD types.

OP question:
int *p;
p = new int[10];

void *q = (void*)p;
delete[] q;

Is this valid if p is guaranteed to be a POD? ...


It's never valid. You must pass delete[] the exact same
type and value you got from new [].

The only time you can pass something to delete that's a
different type than what was new is for the single object
case where you have a base class pointer with a derived
destructor. Even then you can't use void*. It has to
be a base class, not any pointer you converted.
Nov 1 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.