• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
problem overriding operator new/delete and std stream
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

problem overriding operator new/delete and std stream


  • Subject: problem overriding operator new/delete and std stream
  • From: Elron Yellin <email@hidden>
  • Date: Fri, 27 Jan 2006 13:39:09 -0500

I'm trying to replace global operator new and delete with my own
versions, but there's a problem with std stream classes.  When I new
up an ostringstream, my version of new is used, but when I delete it,
seems the libstdc++ version of delete is being used.  Any clues?

Thanks.
Elron

#include <new>
#include <sstream>
#include <cstdio>

void* operator new(std::size_t size)
{
	std::printf("my new\n");
	if (size == 0)
		size = 1;

	void* rawMem = std::malloc(size + 16);
	if (rawMem == 0)
		throw std::bad_alloc();
	return rawMem;
}
void* operator new[](std::size_t size)
{
	std::printf("my new[]\n");
	if (size == 0)
		size = 1;

	void* rawMem = std::malloc(size + 16);
	if (rawMem == 0)
		throw std::bad_alloc();
	return rawMem;
}

void operator delete(void* ptr) throw()
{
	std::printf("my delete\n");
	if (ptr)
		std::free(ptr);
}
void operator delete(void* ptr, const std::nothrow_t&) throw()
{
	std::printf("my delete nothrow\n");
	if (ptr)
		std::free(ptr);
}
void operator delete[](void* ptr) throw()
{
	std::printf("my delete[]\n");
	if (ptr)
		std::free(ptr);
}
void operator delete[](void* ptr, const std::nothrow_t&) throw()
{
	std::printf("my delete[] nothrow\n");
	if (ptr)
		std::free(ptr);
}

struct foo
{
	foo() { std::printf("foo()\n"); }
	~foo() { std::printf("~foo()\n"); }
};

int main (int argc, char * const argv[]) {

	foo* f = new foo;
	delete f;

	std::ostringstream* s = new std::ostringstream;
	delete s;

    return 0;
}

Output:
my new
foo()
~foo()
my delete
my new

Debugger stopped.
Program exited with status value:0.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: NSBox subclass hangs IB when attempting to add view to the box.
  • Next by Date: Re: Debugging a CGI with XCode, is it possible?
  • Previous by thread: Re: NSBox subclass hangs IB when attempting to add view to the box.
  • Next by thread: debug MPI apps via Xcode?
  • Index(es):
    • Date
    • Thread