This is the mail archive of the
mailing list for the newlib project.
Re: sprintf() heap usage
- From: Joel Sherrill <joel dot sherrill at oarcorp dot com>
- To: Eric Blake <eblake at redhat dot com>
- Cc: Vasili Galka <vvv444 at gmail dot com>, "newlib at sourceware dot org" <newlib at sourceware dot org>
- Date: Tue, 16 Jul 2013 10:23:48 -0500
- Subject: Re: sprintf() heap usage
- References: <CA+gZxsO8PrqwaTU-xMJ1QrOvz3otf_uVt2ap-wGtdKj7niX7GQ at mail dot gmail dot com> <51E56435 dot 60408 at redhat dot com>
On 7/16/2013 10:18 AM, Eric Blake wrote:
On 07/16/2013 03:39 AM, Vasili Galka wrote:
I've been surprised to discover that using sprintf() leads to
requirement of sbrk(). Can anyone please explain me why?
For gods sake, the function already has output buffer provided. The
lifetime of the function is well defined and it has stack. Why would
it require heap!?
Computation of %g and friends can require allocation in order to safely
convert a power-of-two floating-point number into a power-of-10 string
representation (particularly for numbers on the extreme small end, like
1e-300). Even if the final representation fits in the buffer passed
into snprintf, the intermediate conversion steps require more bytes than
can be safely allocated within a single page of the stack, and as newlib
cannot assume a large stack size, it is easier to implement the
conversion process using the heap.
And none of this requires virtual memory, paging, or an MMU. It is simply
making the heap larger.
Joel Sherrill, Ph.D. Director of Research & Development
joel.sherrill@OARcorp.com On-Line Applications Research
Ask me about RTEMS: a free RTOS Huntsville AL 35805
Support Available (256) 722-9985