::new causes turn-off if out of memory

Post Reply
Jens
Posts: 21
Joined: Tue Jun 09, 2009 5:13 am
Contact:

::new causes turn-off if out of memory

Post by Jens » Mon Jul 19, 2010 5:27 am

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?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

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

Post by elhobbs » Mon Jul 19, 2010 11:54 am

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) {	
}

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

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

Post by WinterMute » Mon Jul 19, 2010 12:36 pm

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.
Help keep devkitPro toolchains free, Donate today

Personal Blog

User avatar
fincs
( ͡° ͜ʖ ͡°)
Posts: 94
Joined: Mon Jul 12, 2010 9:45 pm
Location: Seville, Spain
Contact:

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

Post by fincs » Mon Jul 19, 2010 3:46 pm

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/
Donate to devkitPro - help us stay alive!

Jens
Posts: 21
Joined: Tue Jun 09, 2009 5:13 am
Contact:

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

Post by Jens » Fri Jul 23, 2010 7:14 am

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. :)

Discostew
Posts: 103
Joined: Sun Mar 08, 2009 7:24 pm

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

Post by Discostew » Fri Jul 23, 2010 4:54 pm

I'm so glad I check up on these kind of threads. Lot of memory to be saved by following those guidelines.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 18 guests