This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: sprintf() heap usage


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 07/16/2013 05:23 PM, Vasili Galka wrote:
> I see. Thank you!
> 
> On Tue, Jul 16, 2013 at 6:18 PM, Eric Blake <eblake@redhat.com>
> wrote:
>> 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.

You may want to try siprintf(). It's a newlib-specific function that
doesn't support floating point numbers, but has a reduced code size.
Now, I haven't actually checked that it doesn't use the heap (as I
tend to use malloc also in deeply embedded systems), but it might be
worth a try.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJR5Yv6AAoJECkLFtN5Xr9f3W0H/jRRlzpJEGS8X+E15yQdEwOf
srjiTWqiI8d3D8r+WLZ4SimvaGJ4cy39tSdc2FdfRah1rWrwfHx4HaQ8owhy5iX+
usRHF8sYb1l46U3GHyYjUnQgjV2EvHooVFSgmmzwzVH8qiogj4vVtBnPHIylqHpc
8KwN9IK9HlihNVZ3QahuVX3maczFQbmXxHRwqELdF7QWEkfUYvlihI1+wUVmJRWq
qZSEYfX1KAQM4bvKdnG+ZVceJirffRDLHeEgyxE3sTxrDnZLM9w+5uBRxS2c6D5z
eVDeOxwFY5BO3AIYEpWak8mv7J7sMqz7eElcNM6/o8g0boISky6nJ1np7QQ8NYw=
=6cxd
-----END PGP SIGNATURE-----


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]