473,402 Members | 2,055 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,402 software developers and data experts.

double delete

Hello

I know this is not good code, but i was wondering why dubble delete
gives an error (not an exception!!) and dubble free doesn't??
If new/delete works like malloc/free. Is the error caused by the
destructor?? and if so why???
Integer *m = (Integer*)malloc(sizeof(*m));
free(m);
free(m);

Integer *m = new Integer();
delete m;
delete m;
thx
Jul 22 '05 #1
9 2821
onsbomma wrote:
I know this is not good code, but i was wondering why dubble delete
gives an error (not an exception!!) and dubble free doesn't??
If new/delete works like malloc/free. Is the error caused by the
destructor?? and if so why???

Integer *m = (Integer*)malloc(sizeof(*m));
free(m);
free(m);

Integer *m = new Integer();
delete m;
delete m;


Because "undefined behavior" means anything could happen. Anything from the
program behaves the way you hope, to the nearest toilet explodes.

(If more implementations went with the latter, less C++ code out there would
suck.)

Mechanically, maybe free() doesn't call Integer's destructor, and maybe
calling that destructor twice bombs an internal pointer.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2

"onsbomma" <on******@hotmail.com> wrote in message
news:10***************@seven.kulnet.kuleuven.ac.be ...
Hello

I know this is not good code, but i was wondering why dubble delete
gives an error (not an exception!!) and dubble free doesn't??
What sort of error? I don't think the code below should cause a compile
error.

If new/delete works like malloc/free. Is the error caused by the
destructor?? and if so why???
new/delete don't work exactly like malloc/free and the destructor is one
difference. It is possible that the destructor is causing the error
(whatever that is) but it would really depend on how the destructor was
written.


Integer *m = (Integer*)malloc(sizeof(*m));
free(m);
free(m);

Integer *m = new Integer();
delete m;
delete m;


Both if these pieces of code invoke what is known as undefined behaviour. It
is a concept that some people have trouble getting to grips with, but
basically it says that if you do something that is against the rules of C++
(in this case freeing/deleting the same memory twice) then the behaviour of
your code is completely undefined. It might crash, it might output an error,
it might throw an exception, it might format you hard drive, etc. etc.
Anything might happen, INCLUDING it might work.

john
Jul 22 '05 #3
Integer *m = (Integer*)malloc( ...

Integer* p_m = reinterpret_cast< Integer* const & > ( malloc(...
-JKop
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL> wrote in message
news:kf*******************@news.indigo.ie...
Integer *m = (Integer*)malloc( ...

Integer* p_m = reinterpret_cast< Integer* const & > ( malloc(...
-JKop


Why are you casting a void* (from malloc) to a const reference to an
Integer* (whatever the heck an "Integer" is)? Why not just cast to an
Integer*, since that's the type being assigned to?

-Howrad
Jul 22 '05 #5
JKop <NU**@NULL.NULL> wrote in news:kf*******************@news.indigo.ie:
Integer *m = (Integer*)malloc( ...

Integer* p_m = reinterpret_cast< Integer* const & > ( malloc(...


Why you want want to ever obfuscate your code like that? What is the
"const &" portion really contributing to the expression (functionally, not
the langauage definition.)? Or, as a rephrase, why in this specific
instance is the const ref desirable and/or necessary? What additional and
useful expressiveness does your method have over:

Integer * m = reinterpret_cast<Integer *>(malloc(sizeof(Integer)));

(I would hope there would be a placement new call somewhere later.....)
Jul 22 '05 #6
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:kf*******************@news.indigo.ie...
Integer *m = (Integer*)malloc( ...

Integer* p_m = reinterpret_cast< Integer* const & > ( malloc(...
-JKop


Why are you casting a void* (from malloc) to a const reference to an
Integer* (whatever the heck an "Integer" is)? Why not just cast to an
Integer*, since that's the type being assigned to?

-Howrad

reinterpret_cast< Integer* const & >

reinterpret_cast< Integer* >

reinterpret_cast< Integer * const >
Either will do.
I suppose I chose the first as it's the most "complete" in that it
yields an l-value. There's also less likelyhood of the formation of a
temporary.
-JKop

Jul 22 '05 #7
JKop wrote in news:kf*******************@news.indigo.ie in comp.lang.c++:
Integer *m = (Integer*)malloc( ...

Integer* p_m = reinterpret_cast< Integer* const & > ( malloc(...


Your replacment for the well formed static_cast isn't legal C++
you can't cast an rvalue to an lvalue.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #8

"JKop" <NU**@NULL.NULL> wrote in message
news:OX*******************@news.indigo.ie...
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:kf*******************@news.indigo.ie...

Integer *m = (Integer*)malloc( ...
Integer* p_m = reinterpret_cast< Integer* const & > ( malloc(...
-JKop
Why are you casting a void* (from malloc) to a const reference to an
Integer* (whatever the heck an "Integer" is)? Why not just cast to an
Integer*, since that's the type being assigned to?

-Howrad

reinterpret_cast< Integer* const & >

reinterpret_cast< Integer* >

reinterpret_cast< Integer * const >
Either will do.
I suppose I chose the first as it's the most "complete" in that it
yields an l-value. There's also less likelyhood of the formation of a
temporary.


I don't understand this at all. You're simply initializing a pointer. The
call to malloc returns a void*, which is used to directly initialize an
Integer*. There are no constructors or anything involved here that might
cause unwanted copies of objects to be created. And what do you mean that
it "yields an l-value"? And how does that help anything?

I think you've gotten WAY too hung up on references and const. Keep It
Simple, umm, Sir. :-)

(By the way, isn't a static_cast the normal cast used with malloc? Assuming
you've got some reason to not use new in the first place, that is?)

-JKop

Jul 22 '05 #9
Howard wrote:
[redacted]

reinterpret_cast< Integer* const & >

reinterpret_cast< Integer* >

reinterpret_cast< Integer * const >

[redacted]


why not use static_cast? A void* can be static_cast'ed to any other
pointer type. i.e.

// do not this. use new Integer or new Integer[...] instead!!!!
Integer* some_ptr = static_cast<Integer*>(malloc(...));
Jul 22 '05 #10

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

Similar topics

17
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
6
by: Matan Nassau | last post by:
Hello. i have a composite which i want to delete. this is a composite which represents a boolean expression (see a previous post of mine with more details at...
1
by: davegraham_1998 | last post by:
Hi All- I'm pretty sure this have been discussed earlier, but couldn't find a solution to my problem. <input type="submit" value="Delete Parts" onclick="return handleDeleteParts('ABC', 'MY...
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
20
by: Martijn | last post by:
Hi, Those familiar with Windows programming may be familiar with the windowsx.h header. In this header macros exist that use double casts, like so (hope this is readable): #define...
2
by: PRadyut | last post by:
in this code the delete function does not delete the last node of the double linked list the code ---------------------------------------------------------------- #include <stdio.h>...
0
by: Frank King | last post by:
Hi, I am using CArray and quick sort funciton to sort an array of double type of data points. I found an article in MSDN HOWTO: Quick Sorting Using MFC CArray-Derived Classes ID: Q216858 ...
1
by: paulcybulski | last post by:
I am trying to create a function where if a user double clicks on a particular mc the mc will reset itself to its original zoom and position...however, I cannot get this feature to work...
5
by: =?Utf-8?B?YmVlamF5?= | last post by:
Whilst attempting to delete files in a folder all I get is a double up of the original file without any deletion; I now have at least 9 copies of the original file!!! Please can someone help? --...
3
by: kingparthi | last post by:
This is a program to add, display & delete an item from the double linked list ... Here the add & display works correctly... where my delete is having some problem, when ever I delete an item, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.