tags:

views:

786

answers:

5

I know that I am supposed to use delete [] when I used new [], so using auto_ptr with new [] is not such a bright idea.

However, while debugging delete [] (using Visual Studio 2005) I noticed that the call went into a function that looked like this:

void operator delete[]( void * p )
{
    RTCCALLBACK(_RTC_Free_hook, (p, 0))
    operator delete(p);
}

Does this mean, the [] syntax is lost on Visual C++? If so, why? Is it to relieve the developer from the burden of remembering the right syntax?

+2  A: 

Just because they might work the same now, doesn't mean you can necessarily treat them the same - the behaviour could change. And besides, who cares - you should write your code so you don't have to remember.

Additionally, you can use scoped_array for array deletion.

1800 INFORMATION
I know, we should, but so far we don't use Boost. :(
HS
I believe scoped_array (and other Boost smart pointer types) are part of TR1. You may already have them without Boost.
Ferruccio
I missed the part about VS2005. I think TR1 is in VS2008 SP1.
Ferruccio
+3  A: 

I guess it's just an implementation detail. Their heap allocator works the same way when freeing arrays and pointers.

But since the standard allows implementations to have different algorithms for the two cases, you really shouldn't assume that delete and delete[] do the same thing. The behaviour might even change between compiler versions.

CAdaker
+2  A: 

Perhaps the data you deleted did not have destructors? If so, this simple delete would make sense. It is working on void* after all.

Anyway, you're supposed to use delete[] to make sure that the destructor is run for each element in the array.

Jørn Jensen
+1  A: 

Deleting an array without [] will only free the memory, but will NOT call the destructors in each object in the array. So, technically you only need [] if your destructor needs to be called.

Magnus Westin
I would definitely revise your first statement as it is not correct and dangerous even though you give a correct explanation at the end.
Andreas Magnusson
You are correct, VS 2005 actually do care. And they even detect the problem during run time. I tested this a long time ago, so I guess MS have changed it.
Magnus Westin
+21  A: 

Consider this code:

class DeleteMe
{
public:
  ~DeleteMe()
  {
    std::cout << "Thanks mate, I'm gone!\n";
  }
};

int main()
{
  DeleteMe *arr = new DeleteMe[5];
  delete arr;
  return 0;
}

If you run that in VS2005 it will print:

Thanks mate, I'm gone!

If you change main() to correctly adhere to the C++ standard:

int main()
{
  DeleteMe *arr = new DeleteMe[5];
  delete[] arr;
  return 0;
}

It will print:

Thanks mate, I'm gone!
Thanks mate, I'm gone!
Thanks mate, I'm gone!
Thanks mate, I'm gone!
Thanks mate, I'm gone!

Don't shoot yourself in the foot. VS2005 will NOT do the correct thing if you mismatch different flavors of new/delete. Neither will any other C++ standard conformant compiler.

There's some compiler magic going on around operator new and operator delete (and their different flavors), basically the call to the ctors and dtors are added behind the scenes. This magic depends on those small brackets [], so don't lose them or you'll lose the magic.

Andreas Magnusson
OK, I accept that it is done via magic. :)
HS