What does the following code do (in an ARM-compliant compiler)?

	T* p1 = new T;
	T* p2 = new T[100];
	
	delete p2;
	delete[] p1;

In the best case, delete p2 will probably just delete one T element leaving 99 orphaned and delete[] p1 might not crash your machine!

This is because, under the ARM, the housekeeping information for single objects and arrays of objects was likely to be different - the compiler keeps track of how many elements are allocated with new[] so that the delete[] knows how many elements to free up!


In ISO C++, you define separate versions of new and delete that get called for array allocation and deallocation. This allows finer control over storage allocation:

	class T
	{
	public:
		// scalar new and delete:
		void*	operator new(size_t);
		void	operator delete(void*);
		// array new and delete:
		void*	operator new[](size_t);
		void	operator delete[](void*);
		// ...
	};

	T* tp = new T;		// calls T::operator new()
	T* tap = new T[100];	// calls T::operator new[]()
	delete tp;		// calls T::operator delete()
	delete [] tap;		// calls T::operator delete[]()

Note that you must be careful to use delete [] only on storage that was allocated with new [], and delete only on storage that was allocated with new.