|
From: jody <jod...@gm...> - 2012-10-02 15:58:52
|
I must admit this null checking of mine is also an old school left-over...
On Tue, Oct 2, 2012 at 5:16 PM, David Chapman <dcc...@ac...> wrote:
> On 10/2/2012 7:33 AM, Patrick J. LoPresti wrote:
>>
>> On Tue, Oct 2, 2012 at 5:55 AM, jody <jod...@gm...> wrote:
>>>
>>>
>>> ---------------------------------
>>> void CellGrid::destroyGrid() {
>>>
>>> if (m_apCells != NULL) {
>>> for (coord j = 0; j < m_iNumCells; j++) {
>>> if (m_apCells[j] != NULL) {
>>> delete m_apCells[j];
>>> }
>>> }
>>> delete[] m_apCells;
>>> }
>>>
>>> //.... other stuff unrelated to m_apCells
>>> }
>>
>> This is not related to your problem, but... In C++ -- all versions
>> ever, standard or otherwise -- calling "delete" on a NULL pointer is
>> guaranteed to be a no-op. So there is never a need to check for NULL
>> before calling "delete". Doing so makes your code run slower, makes
>> it harder to read, and makes the reader wonder whether the author
>> simply does not know C++ or is trying to work around some obscure bug
>> in some compiler.
>>
>> - Pat
>>
>
> It wasn't so obscure 20 years ago - programs compiled with Zortech C++ would
> crash if you called a virtual destructor for a NULL pointer. That wasn't
> fixed for a couple of versions. Fortunately, all C++ compilers of which I
> am aware today handle this properly and the NULL check is truly redundant.
> But I still find "can't delete 0 in Zortech" comments in some of my oldest
> C++ code.
>
> And now back to your regularly scheduled programming...
>
> --
> David Chapman dcc...@ac...
> Chapman Consulting -- San Jose, CA
> Software Development Done Right.
> www.chapman-consulting-sj.com
>
|