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

Using NEW and DELETE operators?

Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work. I want to delete the
pointer only if it's a valid pointer.

How to do that?

Oct 25 '07 #1
7 2753
In article <11**********************@o80g2000hse.googlegroups .com>,
Donos <do******@gmail.comwrote:
Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work. I want to delete the
pointer only if it's a valid pointer.

How to do that?
your original code will do that:

delete [] pChar; // will delete pChar only if it is not NULL
Oct 25 '07 #2
On 2007-10-26 00:03, Donos wrote:
Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work.
In what way does it not work? It works just fine for me.
I want to delete the pointer only if it's a valid pointer.
How to do that?
Why? Using delete on a null-pointer is a no-op, performing the check is
just a waste of code and execution time.

--
Erik Wikström
Oct 25 '07 #3

"Erik Wikström" <Er***********@telia.comwrote in message
news:Hy*****************@newsb.telia.net...
On 2007-10-26 00:03, Donos wrote:
>Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work.

In what way does it not work? It works just fine for me.
>I want to delete the pointer only if it's a valid pointer.
How to do that?

Why? Using delete on a null-pointer is a no-op, performing the check is
just a waste of code and execution time.
He's showing a test for null which isn't needed since it would
be a NOOP as you say. But he's saying that he wants to
call delete on pChar only if it's a VALID pointer. I suspect
he wants to prevent deleting an uninitialized pointer.

Oct 25 '07 #4
On Oct 26, 12:13 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-26 00:03, Donos wrote:
I want to delete the pointer only if it's a valid pointer.
How to do that?
Why? Using delete on a null-pointer is a no-op, performing the check is
just a waste of code and execution time.
It's more a waste of programmer time; a good compiler could
optimize the check out, and even if it didn't, the difference in
execution time is not significant.

But his question stands. He didn't say he wanted to delete the
pointer only if it's non-null; he said he wanted to delete it
only if it's valid. And testing for null doesn't work, because
invalid pointers don't necessarily compare equal to null.
(Formally, even trying to compare an invalid pointer with null
results in undefined behavior. In practice, however, it will
usually just compare unequal---just like a valid pointer to an
object would.)

As for the answer to his question: you need some tool external
to C++, like Purify or valgrind, to do this. Such tools impose
a very significant runtime and space overhead, however, there
are probably licensing issues if you want to deliver the
software with the tool, and they generally don't convert the
operation to a no-op, but rather generate error messages, etc.

For pure memory management, of course, he can use the Boehm
collector, and forget the delete's entirely. This doesn't work
with objects which have a managed lifetime, of course, but
typically, if you're just conditionally deleting like he seems
to be, the object doesn't have a managed lifetime anyway.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 26 '07 #5
James Kanze <ja*********@gmail.comwrote:
He didn't say he wanted to delete the
pointer only if it's non-null; he said he wanted to delete it
only if it's valid.
As for the answer to his question: you need some tool external
to C++, like Purify or valgrind, to do this.
Another option would be to write a smart pointer that does the right
thing.
Oct 26 '07 #6
"Donos" <do******@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work. I want to delete the
pointer only if it's a valid pointer.

How to do that?
Your code is most likely working. However, delete[] pChar does not change
the value of the pointer, it sitll points to some memory address (now
invalid). Perhaps you are deleting it, then attempting to delete it again.
You may just need to:

delete[] pChar;
pChar = NULL;

The check for NULL is not required. delete already does that check, but
only for a NULL value, any other value (even invalid) it will attempt to
delete.
Oct 26 '07 #7
On Oct 26, 12:29 pm, "Daniel T." <danie...@earthlink.netwrote:
James Kanze <james.ka...@gmail.comwrote:
He didn't say he wanted to delete the
pointer only if it's non-null; he said he wanted to delete it
only if it's valid.
As for the answer to his question: you need some tool external
to C++, like Purify or valgrind, to do this.
Another option would be to write a smart pointer that does the right
thing.
Even before that, you have to define what is meant by "the right
thing".

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 27 '07 #8

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

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
4
by: Stefan Strasser | last post by:
why is delete an expression and not a statement? (in my draft copy of the standard). I was about to ask the same question about "throw" but found an expression case of throw("return boolvalue ? 5...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
9
by: groleo | last post by:
Hi list. Simple question: is it possible to override the global new/delete operators, without using malloc/free? I mean something in the ideea of the code below, which doesnt work cause of...
5
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class...
1
by: dilabox | last post by:
Hello, I have overloaded the global new, delete, new and delete operators inside a "static library". The library uses private memory allocation routines that must not be accessible from other...
1
debasisdas
by: debasisdas | last post by:
Using Co-related sub query ======================== While a subquery is evaluated only once for each table, a correlated subquery is evaluated once for each row. Sub query can take value from...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.