Archived

This topic is now archived and is closed to further replies.

overloading delete[] problems

This topic is 5049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve played with this code:
class test
{
public:
	void* operator new( size_t sz )
	{
		cout << "Allocating " << sz << " bytes" << endl;
		return malloc( sz );
	}

	void* operator new[]( size_t sz )
	{
		cout << "Allocating " << sz << " bytes" << endl;
		return malloc( sz );
	}

	void operator delete( void* ptr, size_t sz )
	{
		cout << "Freeing " << sz << " bytes" << endl;
		free( ptr );
	}

	void operator delete[]( void* ptr, size_t sz )
	{
		cout << "Freeing " << sz << " bytes" << endl;
		free( ptr );
	}

	char onebyte;
};

int main( void )
{
	delete (new test);
	delete (new test[5]);
}
The program outputs
Allocating 1 bytes
Freeing 1 bytes
Allocating 5 bytes
Freeing 1 bytes
[code]

Which I find very odd. Can anyone explain this phenomena?   
-Luctus
Statisticly seen, most things happens to other people. [Mail]

Share this post


Link to post
Share on other sites
delete recieves the size of the datatype as an argument, not the size of an array. If you overload new[] and delete[], you are expected to keep track of the size of the array yourself, usually by putting it right before the allocated block. (Incidentally, this is why mixing up delete and delete[] is a bad idea).


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Your code for new and new[], and delete and delete[] are exactly the same.

When you call new[] you are not allocating an array you are simply creating a pointer to the type.

Share this post


Link to post
Share on other sites
Ok, I suspected as much, but according to this site (section 9.8) says
quote:

Operator delete[] may be overloaded using extra parameters. However, if overloaded as
void *operator delete[](void *p, size_t size)

then size is automatically initialized to the size (in bytes) of the block of memory to which void *p points.


Is he just talking about something he doesn''t know? But since the document is from an university, one would thing it should be correct.(but still, returning a void* sounds wrong)

-Luctus

Statisticly seen, most things happens to other people.
[Mail]

Share this post


Link to post
Share on other sites
cowper: really? The new[] operator function alloctes 5 bytes of memory. Isn't that's exactly what an array is? (how on earth should one otherwise allocate an array on the heap in C?)

//edit
tried changing the code to this:

class test
{
public:
void* operator new( size_t sz )
{
cout << "Allocating " << sz << " bytes" << endl;
return ::new test;
}

void* operator new[]( size_t sz )
{
cout << "Allocating " << sz << " bytes" << endl;
return ::new test[sz/sizeof(test)];
}

void operator delete( void* ptr, size_t sz )
{
cout << "Freeing " << sz << " bytes" << endl;
::delete ptr;
}

void operator delete[]( void* ptr, size_t sz )
{
cout << "Freeing " << sz << " bytes" << endl;
::delete[] ptr;
}

char onebyte;
};

which still produced the same results;

-Luctus

Statisticly seen, most things happens to other people.
[Mail]


[edited by - Luctus on February 20, 2004 7:01:13 PM]

Share this post


Link to post
Share on other sites