[haiku-development] Re: DeskCalc Improvements (was need strtold() function)

  • From: Stephan Assmus <superstippi@xxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Thu, 31 Dec 2009 20:12:50 +0100

On 2009-12-31 at 17:40:05 [+0100], John Scipione <jscipione@xxxxxxxxx> 
wrote:
> My thinking in writing the NULL check was that I 
> wanted to check if you don't have enough memory to make a char 
> buffer[decimalPlaces + 15]. I mean, if decimalPlaces is set to MAX_INT 
> than that is ~32k of memory which is sort of a lot.

Ok, it actually is a lot then. But what happens is that you allocate the 
memory on the threads stack (IIRC, Haiku threads have 256 K of stack 
memory). That you can do this at all is a GCC feature, since the standard 
does not support dynamic stack allocations (at least not explicitely, if 
memory serves). So "buffer" will never be NULL (always a stack address), 
but one can still run out of stack space, and that's called a stack 
overrun. What you should do is allocate the buffer with "new(std::nothrow)" 
or "malloc". And then keep the check of course, because these functions 
would then indeed return NULL if the allocation failed.

Best regards,
-Stephan

Other related posts: