Is this a valid delete

N

Ninan

I am skipping a couple of steps in betweens, like inserting into a list
of ints and extracting from it. But this is the gist of what I am
trying to do

class A {

};

A *a = new A();

int b = (int )a;

delete (A *)b;

Is this equivalent to
delete a?

My code seems to work fine
 
P

Phlip

Ninan said:
I am skipping a couple of steps in betweens, like inserting into a list
of ints and extracting from it. But this is the gist of what I am
trying to do

class A {

};

A *a = new A();

int b = (int )a;

delete (A *)b;

Is this equivalent to
delete a?

Tip: Replace all C-style casts with C++ casts.

In this case, you must use reinterpret_cast<>.

I suspect the Standard requires your compiler to define the act of
reinterpretting something to another type, and then back to the same type.
If so, then the delete is valid.
 
R

roberts.noah

Phlip said:
Tip: Replace all C-style casts with C++ casts.

In this case, you must use reinterpret_cast<>.

I suspect the Standard requires your compiler to define the act of
reinterpretting something to another type, and then back to the same type.
If so, then the delete is valid.

Yes, the standard states that reinterpret_cast from a pointer to an
integral and then back to the same pointer results in a pointer of the
original value if the integral is large enough to hold the value of the
pointer. This is in 5.2.10 parts 4&5.

On the other hand, I don't know that reinterpret_cast is guaranteed to
be the operation of the above C-Style casts. Could be...better just to
use C++ style casts and be positive of what is going on.
 
V

Victor Bazarov

Phlip said:
Ninan wrote:




Tip: Replace all C-style casts with C++ casts.

In this case, you must use reinterpret_cast<>.

I suspect the Standard requires your compiler to define the act of
reinterpretting something to another type, and then back to the same type.
If so, then the delete is valid.

The only potential problem is that a pointer to A may not fit into an int.
You _have_ to pick an integral value _large_enough_. Just think 32-bit
ints on a 64-bit system (with 64-bit pointers).

V
 
P

peter koch

Ninan skrev:
I am skipping a couple of steps in betweens, like inserting into a list
of ints and extracting from it. But this is the gist of what I am
trying to do

class A {

};

A *a = new A();

int b = (int )a;

delete (A *)b;

Is this equivalent to
delete a?

Nope. This is not guaranteed to work. There is no guarantee that a
pointer can be represented in an integer. Most probably there will be
some integer type that can be used to keep a pointer, but I doubt that
this is guaranteed by the standard.
I wonder what rationale you have to "disguise" your pointers the way
you do - perhaps it could be some opaque interface?

/Peter
 
N

Ninan

peter said:
Ninan skrev:


Nope. This is not guaranteed to work. There is no guarantee that a
pointer can be represented in an integer. Most probably there will be
some integer type that can be used to keep a pointer, but I doubt that
this is guaranteed by the standard.
I wonder what rationale you have to "disguise" your pointers the way
you do - perhaps it could be some opaque interface?

/Peter
I should confess that I am not the inventor of this code. It belongs to
someone who left the job and whose work I have taken over
But rationale is some thing like this. The pointers to classes are
inserted into a list of integers. At a future point in time the
contents of the classes are converted (streamed) to socket messages and
sent over the network. At this point the pointer to the classes are
deleted from the list. One advantage I can see is that any type of
class can be inserted into this list, though it is not being done at
present
 
J

jolest

Ninan said:
I should confess that I am not the inventor of this code. It belongs to
someone who left the job and whose work I have taken over
But rationale is some thing like this. The pointers to classes are
inserted into a list of integers. At a future point in time the
contents of the classes are converted (streamed) to socket messages and
sent over the network. At this point the pointer to the classes are
deleted from the list. One advantage I can see is that any type of
class can be inserted into this list, though it is not being done at
present

As someone pointed out, while you may be currently "getting away with
it", this is not guarenteed to work on all flavors of C++.

If you want conformant code, and all you're trying to do is to have an
array that can contain pointers to any kind of object, why not use void
pointers?

The following typedef may help:
typedef void* VOID_PTR;

... Jolest

PS: Obviously, you'll still need to cast it back to the original type
before deleting it...
 
D

Daniel T.

"Ninan said:
I am skipping a couple of steps in betweens, like inserting into a list
of ints and extracting from it. But this is the gist of what I am
trying to do

class A {

};

A *a = new A();

int b = (int )a;

delete (A *)b;

Is this equivalent to
delete a?

My code seems to work fine

Others have mentioned the importance of the size of the integral type, I
would think it's also important that the type be unsigned, but I could
be wrong. It just seems that conversions could trip you up otherwise.

Personally, long ago I wrote a system like that. I used the pointer
(this) to uniquely identify objects at runtime. Once you start newing
and deleting objects though, all bets are off. Another object my come
along that has the same id.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top