snprintf w/o dependecy to malloc

Asked by Thomas Giesel

Is there a way in newlib-nano to use snprintf without pulling in malloc? If not: This would be a big point for the wish list :)

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
chengbin (can-finner) said :
#1

Seems a good point. I will look into the implementation of snprintf to see whether it's possible.

Thanks for the suggestion.

Revision history for this message
Joey Ye (jinyun-ye) said :
#2

Change status to answered

Revision history for this message
chengbin (can-finner) said :
#3

Hi, the malloc stuff is pulled in for asnprintf function. For both newlib and newlib-nano, snprintf shares this piece of code with asnprintf.
It's possible to not to pull in malloc for snprintf with more complicated code, but considering _malloc_r is only about 150 bytes, I am not very convinced that it should be done.

Any comments? Thanks.

Revision history for this message
TM (tm1234) said :
#4

Is my reply here of any use in this context?

https://answers.launchpad.net/gcc-arm-embedded/+question/233446

Hope this helps.

Revision history for this message
Thomas Giesel (skoe) said :
#5

Thank you for the comments.

TM, yes, that question lists one aspect why dynamic memory allocation may not be wanted: On systems with very limited resources.

Another reason: There are software projects where dynamic memory allocation is simply not allowed, for example because of safety and/or deterministic reasons. One could argue that newlib is not the right candidate for these projects, but as long as there are no legal nor technical reasons not to use it and as long as the resulting code is tested very well, it may make sense.

Revision history for this message
Krzysztof Wesolowski (krzysztof-wesolowski) said :
#6

And third group - simple projects where it is not needed by other parts of system, and providing malloc or sbrk only to use printf can be avoided.

Can you help with this problem?

Provide an answer of your own, or ask Thomas Giesel for more information if necessary.

To post a message you must log in.