Page 1 of 1
C++ new operator return values
Posted: Mon Jul 31, 2006 1:07 am
by Neo
What happens when 'new' is not able to allocate enough memory?
Does it return NULL like the alloc() functions or does it only throw an exception?
Re:C++ new operator return values
Posted: Mon Jul 31, 2006 2:06 am
by Solar
Default behaviour is to throw a bad_alloc exception. I've heard about certain settings you could make to change that behaviour to e.g. returning NULL, but haven't given them a try so far.
Note that most UNIXish operating systems will never throw such an exception, but return successfully at first (when they do the page table entries) and die violently later (when the page fault handler tries to actually allocate physical pages)...
Re:C++ new operator return values
Posted: Tue Aug 08, 2006 8:16 am
by Pype.Clicker
Solar wrote:
Note that most UNIXish operating systems will never throw such an exception, but return successfully at first (when they do the page table entries) and die violently later (when the page fault handler tries to actually allocate physical pages)...
So there's nothing catching the SIGSEGV and throwing a "bad_alloc" exception instead ??
this also reminds me of a rule-of-thumb i read somewhere about OOP:
"if you thing something might fail, don't do it in your constructor"
Re:C++ new operator return values
Posted: Thu Aug 17, 2006 4:46 am
by distantvoices
@solar: don't unix-like systems create a virtual memory entry for the appended heap and map in the pages upon need (page fault handler doing the grunt work so to say)?
Re:C++ new operator return values
Posted: Thu Aug 17, 2006 5:07 am
by Solar
That's what I said. The actual out-of-(swap)-memory condition happens long after the (successful) malloc / new call.