sprintf() heap usage

Eric Blake eblake@redhat.com
Tue Jul 16 15:18:00 GMT 2013


On 07/16/2013 03:39 AM, Vasili Galka wrote:
> Hi,
> 
> 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.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 621 bytes
Desc: OpenPGP digital signature
URL: <http://sourceware.org/pipermail/newlib/attachments/20130716/ab2ac0e2/attachment.sig>


More information about the Newlib mailing list