4 4752
"Alex Vinokur" <al****@users.sourceforge.net> schrieb im Newsbeitrag news:11*********************@v46g2000cwv.googlegro ups.com... Should 'delete below (after casting to void*)' work fine? ------------------------------------------ char* p1 = new (nothrow) char[100]; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1); delete p2; ------------------------------------------
No. The argument passed to delete must be something returned by new. Your call to new returns a char*, not a void*. The compiler has to know what to delete in order to call appropriate destructors. So, no, you have undefined behaviour. Even if wou would use "delete p1" instead, you would still have undefined behaviour, because not even the value of p1 has been returned by new. You used new[] to allocate your memory, so you should also use delete[] to release it. However, "delete[] reinterpret_cast<char*>(p2)" should work in the example above.
HTH
Heinz
Alex Vinokur wrote: Should 'delete below (after casting to void*)' work fine?
More to the point, why do it?
--
Ian Collins.
Heinz Ozwirk wrote: "Alex Vinokur" <al****@users.sourceforge.net> schrieb im Newsbeitrag news:11*********************@v46g2000cwv.googlegro ups.com... Should 'delete below (after casting to void*)' work fine?
------------------------------------------ char* p1 = new (nothrow) char[100]; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1); delete p2; ------------------------------------------
No. The argument passed to delete must be something returned by new. Your call to new returns a char*, not a void*. The compiler has to know what to delete in order to call appropriate destructors. So, no, you have undefined behaviour. Even if wou would use "delete p1" instead, you would still have undefined behaviour, because not even the value of p1 has been returned by new. You used new[] to allocate your memory, so you should also use delete[] to release it. However, "delete[] reinterpret_cast<char*>(p2)" should work in the example above.
HTH Heinz
Sorry, of course it should be
--- A ---
char* p1 = new (nothrow) char;
if (p1 == 0) return;
void* p2 = reinterpret_cast<void*> (p1);
delete p2;
---------
or
--- B ---
char* p1 = new (nothrow) char[100];
if (p1 == 0) return;
void* p2 = reinterpret_cast<void*> (p1);
delete[] p2;
---------
Thanks.
So, the behaviour is undefined in both cases (?)
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
Alex Vinokur <al****@users.sourceforge.net> wrote: --- A --- char* p1 = new (nothrow) char; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1); delete p2; ---------
or
--- B --- char* p1 = new (nothrow) char[100]; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1); delete[] p2; ---------
So, the behaviour is undefined in both cases (?)
Yes.
hth
--
jb
(reply address in rot13, unscramble first) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vladimir Perkic |
last post by:
Hi everybody!
Does anybody know why Borland C++ 5.5 reports errors at line:
CreateWindow ("button", "One",
WS_CHILD| WS_VISIBLE| BS_PUSHBUTTON,
20, 8, 75, 25, hwnd, (HMENU)IDBT_ONE, hInst,...
|
by: Scott Brady Drummonds |
last post by:
Hi, everyone,
I've checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of...
|
by: Aman |
last post by:
Hi,
wrote this piece of code on SunOS 5.9 , compiler g++ 2.95.3
trying to see the byte order of an int or short int by converting to
char* . doesn't work . the char* cpt doesn't seem to be...
|
by: bonham |
last post by:
i m a beginner in C++. I have some questions:
what is static_cast<unsigned>?
i saw someone use static_cast<unsigned> and static_cast<unsigned char>
together. why?
e.g....
|
by: adamrobillard |
last post by:
Hi,
I have always used fopen and FILE* to load and save structures to file.
I am trying to convert all the older code to use proper C++ calls...
the following code works properly but I would...
|
by: Noah Roberts |
last post by:
What steps do people take to make sure that when dealing with C API
callback functions that you do the appropriate reinterpret_cast<>? For
instance, today I ran into a situation in which the wrong...
|
by: J.M. |
last post by:
I have data in a double array of length 2N, which actually represents
complex numbers with real and imaginary parts interlaced. In other words,
elements in this array with even indices represents...
|
by: john |
last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned:
"Maybe your first idea for a two-dimensional vector was something like this:
class Matrix {
valarray< valarray<doublev;
public:
// ...
};
|
by: Alex Vinokur |
last post by:
Hi,
Is it possible to do C++-casting from
const pair<const unsigned char*, size_t>*
to
const pair<unsigned char*, size_t>*
?
Alex Vinokur
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| | |