Page 1 of 1

::new causes turn-off if out of memory

Posted: Mon Jul 19, 2010 5:27 am
by Jens
If you allocate memory using standard C++ operator ::new, but don't have enough, the DS just turns off. I think this was introduced in a recent libnds update. Does anyone know how to override this behavior?

Re: ::new causes turn-off if out of memory

Posted: Mon Jul 19, 2010 11:54 am
by elhobbs
libnds allows for the following function to be created. It is called when the program exits with a non zero value. it needs to be defined as C code to link properly.

Code: Select all

void systemErrorExit(int rc) {	
}

Re: ::new causes turn-off if out of memory

Posted: Mon Jul 19, 2010 12:36 pm
by WinterMute
Or alternatively you can override the new & delete operators so they return NULL instead of throwing an exception.

Code: Select all

#include <cstdlib>

void* operator new (size_t size) {
	return malloc(size);
}

void operator delete (void *p) {
	free(p);
}

And of course you can tell new not to throw the exception.

Code: Select all

#include <new>
char *array = new(std::nothrow) char[4096*1024];
Another option is to catch the exception but, as for systemErrorExit, you won't have the opportunity to deal with the error at the point of failure.

Re: ::new causes turn-off if out of memory

Posted: Mon Jul 19, 2010 3:46 pm
by fincs
If you are using other C++ features like the standard library you might want to read this:

http://www.coranac.com/2009/02/some-int ... code-size/
http://www.coranac.com/2009/11/sizeof-new/

Re: ::new causes turn-off if out of memory

Posted: Fri Jul 23, 2010 7:14 am
by Jens
Holy smokes, that was some good info. I saved 282'624 bytes by uncommenting a define in my TinyXML header that switched the use of iostream to it's own stream and string handler. I would have worked for forever to get that kind of save, so I owe you one! :)

I tried the systemErrorExit, but couldn't get it to work. I used extern "C" for it's declaration in my .cpp file, but I probably did something wrong and will try it again later.

I might consider overriding new and delete as suggested as that also seem to be a way to save some code-size, but it sounds a bit messy. :)

Re: ::new causes turn-off if out of memory

Posted: Fri Jul 23, 2010 4:54 pm
by Discostew
I'm so glad I check up on these kind of threads. Lot of memory to be saved by following those guidelines.