473,765 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1702
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_cas t<>.

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_cas t<>.

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_cas t 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_cas t 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_cas t<>.

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************ **********@v46g 2000cwv.googleg roups.com>,
"Ninan" <ni****@yahoo.c om> 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
1650
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
1482
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
1644
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 supported across all 3 compilers ? I want a template to take a parameter and make it it's friend. <code>
1
1793
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 command with the SQL Delete from ErrorLog where LogID = ? and LogID is a parameter corresponding to a DB field LogID which is an autoinc field (the parameter is set at Int32).
1
1400
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 following SQL: Delete from ErrorLog where LogID = ? LogID is a parameter corresponding to a DB field LogID which is an autoinc field (the parameter is set at Int32).
30
1924
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
1538
by: Virtual_X | last post by:
void st(const char *a) { cout << a; delete(a); } int main () { char foo= "CPP";
4
10914
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 the file is created on the server. I need a regular expression to validate the filename entered prior to this. I have looked on google where i found a couple but they didnt work. Unfortuntely regular expression syntax is a very foriegn langauge to...
1
4480
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 A.WEIGHT=((SELECT WEIGHT FROM DEFINJON.PART_LIST WHERE $COID IN (SELECT $COID FROM DEFINJON.$EXT WHERE $COID_REF = o.$COID)) - o.WEIGHT) WHERE A.$COID IN (SELECT $COID FROM DEFINJON.$EXT WHERE $COID_REF=o.$COID); DELETE FROM DEFINJON.$EXT WHERE...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.