You are here

Defect report: sprintf() with floats just prints 0x00 with no error. | Cypress Semiconductor

Defect report: sprintf() with floats just prints 0x00 with no error.

Summary: 4 Replies, Latest post by Bob Marlowe on 27 Sep 2015 02:22 AM PDT
Verified Answers: 1
Last post
Log in to post new comments.
evjenth_1483236's picture
7 posts

Defect report: sprintf() with floats just prints 0x00 with no error.

This error existed earlier with the nano lib, and the fix was to either:

a. Disable newlib-nano in the linker, or

b. Enable newlib-nano Float Formatting, or

c. Add "-u _printf_float" to the linker command line

Now, none of these fixes does anything anymore.

user_14586677's picture
7646 posts

Prior to Creator 3.1 the stack and heap sizes were fixed by part group

and large. That changed with 3.1 and so you might want to increase them.

From 3.1 release notes -


Default Stack and Heap Sizes
The default stack and heap sizes for new projects have been adjusted to more closely match real-world
requirements. When new projects are created, the stack size is set to be a fraction of the total SRAM in
the target device. The heap is set to a small value - 128 bytes - for all devices. You can, of course, modify
these settings in the Design-Wide Resources System Editor. For example, if you are not using any heap
based memory allocation the heap can safely be set to 0 bytes.
Note that some example projects and application notes were originally created on older versions of PSoC
Creator and use the larger default sizes. These projects will continue to build and run perfectly because
they have sufficient memory allocated. However, if you attempt to reproduce the example yourself in your
own project you may need to adjust the stack and heap. However, the example documentation will not tell
you to do that because the original project did not require the settings to be changed.


Regards, Dana.

evjenth_1483236's picture
7 posts

Unclear how stack and heap size addresses the issue.

user_274749305's picture
76 posts

Quoted from another site:


When a floating-point format specifier is used with sprintf, one of sprintf's subroutines calls malloc. If malloc returns NULL, the subroutine attempts to write to address 0x00000004 at _dtoa_r+0x1C (looking through the source, it looks like multiple malloc calls are affected; the issue was discovered with newlib-nano but may also affect newlib). On the target processor, this memory is read-only, so the CPU jumps to the exception vector. Dereferencing invalid pointers can lead to undefined (or at least unexpected) behavior. If malloc fails, sprintf should return a value indicating that an error occurred.


Micro programming ain't like windows or linux. We don't live in an environment, we are the environment.





user_1377889's picture
9249 posts

OK, converting float to string:

Upgrade to Creator 3.3 (That is the latest version)

In your project set heap to 0x0200 + your own amount for malloc()s

set "Use newlib-nano" to True

set "Use newlib-nano float formatting" to true

use sprintf() with a buffer large enough to hold the resulting string and the terminating \0 character.


- that's it



Log in to post new comments.