This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

asprintf() In IAR

Other Parts Discussed in Thread: MSP430F5529

I am trying to use asprintf() to make a string to transmit, but I keep getting :

Warning[Pe223]: function "asprintf" declared implicitly 

I am using an MSP430F5529 and I have stdio.h included. Using this function makes my life much simpler so I would rather get it working then make a messy work around. Does IAR not support this function?

  • asprintf() is not standardized; it's a GNU and BSD extension.

    MCUs have so little SRAM that a function like this does not really make sense.

    If you really want it, you can easily implement it yourself (call snprintf() to find out how many characters you need, the malloc() them).

  • Life can be fun even when not simple.

    Try find if gcc supports aspfrintf(). Or, try use snprintf() under IAR.
  • Support does not depend on the compiler but on the C run-time library.

    gcc on large OSes uses GNU libc, which supports asprintf().
    gcc on MSP430 uses newlib, which also supports asprintf(). (That implementation of _svfprintf_r() was not originally designed for small microcontrollers, and is quite bloated.)

  • The IAR tools does not support the "asprintf" function, as it's not part of the C standard library.

    I would say that it's a good thing, since it would most likely have gotten you into a lot of trouble on a very small microcontroller like the MSP430.

    Why is that? The "asprintf" function is a function that allocated memory on the heap large enough for the printed string. However, when you don't have much RAM in the first place, you should not allocate anything on the heap. Even if you diligently free every block is allocated memory, you still risk of running out of heap due to something called "fragmentation".

    If you would show some code where the "asprintf" function is used, we can help you to rewrite it in such a way that it works better on a small microcontroller.

    Finally, if you find yourself in a situation where you really, really need an asprintf function (for example, due to a third party library requirement), I can help you to write one, it requires some knowledge of the inner workings of printf.

        -- Anders Lindgren, IAR Systems, Author of the IAR compiler for MSP430

  • Anders,

    Thank you for your reply. I am using it to generate a string of values to send to a cellular board over UART that then goes to a php handler on my server.

    asprintf(&input1, "/handler2.php?S1=%.2f&S2=%.2f&S3=%.2f&S4=%.2f&S5=%.2f&S6=%.2f&S7=%.2f&S8=%.2f&S9=%.2f&S10=%.2f&S11=%.2f&S12=%.2f&S13=%.2f&S14=%.2f&S15=%.2f&S16=%.2f", temps[0], temps[1], temps[2], temps[3],temps[4], temps[5], temps[6], temps[7], temps[8], temps[9], temps[10],temps[11], temps[12], temps[13], temps[14], temps[15]);

    The handler is very rigid so it has to be send in that exact format. The values are also variable and will change every time I create a new string.

  • Can you share some code where the variable "input" is used and, if possible, freed.

    By the way, which IAR runtime library do you use, CLIB or DLIB? (The reason I ask is that the inner workings of the printf-like functions differ between the two libraries).

        -- Anders

**Attention** This is a public forum