snprintf and malloc

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

snprintf and malloc

Trampas Stern
I was using snprintf() and was shocked to find it called malloc(), is there
a version that does not, or will I need to write my own version?

Thanks
Trampas
Reply | Threaded
Open this post in threaded view
|

Re: snprintf and malloc

Dave Nadler-2
On 12/5/2017 11:51 AM, Trampas Stern wrote:
> I was using snprintf() and was shocked to find it called malloc(), is there
> a version that does not, or will I need to write my own version?
Hi Trampas - If you're doing embedded you might find this helpful:
http://www.nadler.com/embedded/newlibAndFreeRTOS.html
Hope that helps!
Best Regards, Dave

--
Dave Nadler, USA East Coast voice (978) 263-0097, [hidden email], Skype
  Dave.Nadler1

Reply | Threaded
Open this post in threaded view
|

Re: snprintf and malloc

Trampas Stern
Thanks!

It looks like the best option is to implement my own snprintf() and avoid
using newlib's.

Trampas

On Tue, Dec 5, 2017 at 12:21 PM, Dave Nadler <[hidden email]> wrote:

> On 12/5/2017 11:51 AM, Trampas Stern wrote:
>
> I was using snprintf() and was shocked to find it called malloc(), is there
> a version that does not, or will I need to write my own version?
>
> Hi Trampas - If you're doing embedded you might find this helpful:
> http://www.nadler.com/embedded/newlibAndFreeRTOS.html
> Hope that helps!
> Best Regards, Dave
>
> --
> Dave Nadler, USA East Coast voice (978) 263-0097, [hidden email], Skype
>  Dave.Nadler1
>
>
Reply | Threaded
Open this post in threaded view
|

Re: snprintf and malloc

Freddie Chopin
On Tue, 2017-12-05 at 15:47 -0500, Trampas Stern wrote:
> It looks like the best option is to implement my own snprintf() and
> avoid
> using newlib's.

Not the best approach in my opinion.

You may always use sniprintf(), which does _NOT_ support any floating
point formats, but it also does _NOT_ use malloc(). Generally in newlib
there are non-floating-point variants of all printf- and scanf-like
functions - just add the "i" right before printf/scanf and you are
done. For example there is iprintf, siprintf, sniprintf, viprintf and
so on.

Regards,
FCh
Reply | Threaded
Open this post in threaded view
|

Re: snprintf and malloc

Trampas Stern
I was using floating point which may be why it is doing malloc().


Trampas

On Tue, Dec 5, 2017 at 4:05 PM, Freddie Chopin <[hidden email]> wrote:

> On Tue, 2017-12-05 at 15:47 -0500, Trampas Stern wrote:
> > It looks like the best option is to implement my own snprintf() and
> > avoid
> > using newlib's.
>
> Not the best approach in my opinion.
>
> You may always use sniprintf(), which does _NOT_ support any floating
> point formats, but it also does _NOT_ use malloc(). Generally in newlib
> there are non-floating-point variants of all printf- and scanf-like
> functions - just add the "i" right before printf/scanf and you are
> done. For example there is iprintf, siprintf, sniprintf, viprintf and
> so on.
>
> Regards,
> FCh
>