 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
MaxMax Guest
|
Posted: Tue Oct 25, 2005 9:29 am Post subject: delete[] of POD |
|
|
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
|
|
| Back to top |
|
 |
Ferdi Smit Guest
|
Posted: Tue Oct 25, 2005 9:37 am Post subject: Re: delete[] of POD |
|
|
MaxMax wrote:
| Quote: | 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: [email]Ferdi.Smit (AT) cwi (DOT) nl[/email]
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Tue Oct 25, 2005 9:46 am Post subject: Re: delete[] of POD |
|
|
* Ferdi Smit:
| Quote: | 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?
|
|
| Back to top |
|
 |
Ferdi Smit Guest
|
Posted: Tue Oct 25, 2005 10:08 am Post subject: Re: delete[] of POD |
|
|
Alf P. Steinbach wrote:
| Quote: |
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: [email]Ferdi.Smit (AT) cwi (DOT) nl[/email]
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Tue Oct 25, 2005 10:30 am Post subject: Re: delete[] of POD |
|
|
* Ferdi Smit:
| Quote: | 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.
| Quote: | 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?
|
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Tue Oct 25, 2005 11:05 am Post subject: Re: delete[] of POD |
|
|
"Ferdi Smit" <Ferdi.Smit (AT) cwi (DOT) nl> wrote
| Quote: |
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 calledn";
}
};
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
|
|
| Back to top |
|
 |
Ferdi Smit Guest
|
Posted: Tue Oct 25, 2005 11:23 am Post subject: Re: delete[] of POD |
|
|
John Carson wrote:
| Quote: | 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
class Test
{
public:
~Test()
{
std::cout << "Destructor calledn";
}
};
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 )
--
Regards,
Ferdi Smit (M.Sc.)
Email: [email]Ferdi.Smit (AT) cwi (DOT) nl[/email]
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
|
|
| Back to top |
|
 |
MaxMax Guest
|
Posted: Sat Oct 29, 2005 12:37 pm Post subject: Re: delete[] of POD |
|
|
| Quote: | 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
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Sat Oct 29, 2005 2:02 pm Post subject: Re: delete[] of POD |
|
|
"MaxMax" <none (AT) none (DOT) com> wrote
: >> 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
|
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Mon Oct 31, 2005 10:54 am Post subject: Re: delete[] of POD |
|
|
"Ivan Vecerina" <INVALID_use_webform (AT) ivan (DOT) vecerina.com> wrote
| Quote: | "MaxMax" <none (AT) none (DOT) com> wrote in message
news:n2K8f.37014$Pe2.675133 (AT) twister2 (DOT) 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? ...
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Tue Nov 01, 2005 1:50 pm Post subject: Re: delete[] of POD |
|
|
Jim Langston wrote:
| Quote: | 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.
| Quote: |
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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|