473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Is this a valid delete

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

Feb 27 '06 #1
7 1684
Ninan wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 27 '06 #2

Phlip wrote:
Ninan wrote:
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.


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.

Feb 27 '06 #3
Phlip wrote:
Ninan wrote:

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.


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
--
Please remove capital As from my address when replying by mail
Feb 27 '06 #4

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
My code seems to work fine


Feb 27 '06 #5

peter koch wrote:
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

My code seems to work fine

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

Feb 27 '06 #6
Ninan wrote:
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...

Feb 28 '06 #7
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Ninan" <ni****@yahoo.com> wrote:
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.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Yongming Wang | last post by:
FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me? class A { public: void FuncA() { B b(this);
4
by: dumboo | last post by:
i m having int *p = NULL; delete p; maybe it is since, i m deleting a null which is valid.. -- _____________________________ Experience is a good teacher..
5
by: Gianni Mariani | last post by:
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1 dies complaining about somthing <unknown>. Is this valid ? More to the point, is there any way of doing this that is...
1
by: Lauchlan M | last post by:
I get the following error. Apart from the fact that it does not make any grammatical sense, what would be likely to be causing it? The background is that nxCmdDeleteErrorLogItem is a delete...
1
by: Lauchlan M | last post by:
I get the following error. Apart from the fact that it does not make any grammatical sense, what would be likely to be causing it? I have a command component 'nxCmdDeleteErrorLogItem' with the...
30
by: Filimon Roukoutakis | last post by:
Suppose that we have a function f(Object*& obj) and have declared a global std::vector<Object*vec; Is it valid to do void g() { vec.push_back(new Object);
12
by: Virtual_X | last post by:
void st(const char *a) { cout << a; delete(a); } int main () { char foo= "CPP";
4
by: bizt | last post by:
Hi, Im looking for a regular expression to check that a entered filename is valid. I have a cms Im building where the user can enter the name of a file, enter content and then click Save where...
1
by: 7h0r | last post by:
When trying to insert this trigger: CREATE TRIGGER ad_des_part_jon AFTER DELETE ON DESIGJON.PART_LIST REFERENCING OLD AS o FOR EACH ROW mode db2sql UPDATE DEFINJON.PART_LIST A SET...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.