overloading delete[] problems

Started by
6 comments, last by Luctus 20 years, 2 months ago
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


Which I find very odd. Can anyone explain this phenomena?   </pre> 

-Luctus
<hr><small><i>Statisticly seen, most things happens to other people.</i>
<a href="mailto:jrehnman@hotmail.com">[Mail]</a></small>   
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Advertisement
Maybe if you used:

delete[] (new test[5]);

You''d get the output you expect ...
--what''s it all about?
You didn''t call delete [], you called delete.
That''s a typo. Even with delete[] (new test[5]) it produces the same output.

-Luctus
Statisticly seen, most things happens to other people.
[Mail]
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
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
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.
--what''s it all about?
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]
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
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]
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams

This topic is closed to new replies.

Advertisement