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.

sprintf() call corrupts my call stack



Hi folks,

I'm finding that printf and sprintf calls are very problematic on the MSP430.  I've gone through the guidelines here:

http://processors.wiki.ti.com/index.php/Tips_for_using_printf

and I've tried playing around with the options for printf support, as well as increasing the stack and heap from 160 bytes to 600.  But I'm still having problems.  As soon as I call sprintf for the first time, my call stack is corrupted.

Any ideas on other configuration changes that might help?

Jimbo

  • Normally a call to (s)printf won't currupt the stack. The caller puts all required data on the stack (2 or 4 bytes - for long - per parameter) and printf only reads it. Also, the subfuncitons of printf may put data on stack jsu tlike normal functions, but don't access anything else. And 600 bytes stack are plenty (I got problems with 128bytes of total ram for stack and variables :) )

    Since you say you 'increased the stack' (which isn't what you've done as there is no mechanism to control the stack size - this 'setting' is just a 'warning threshold' for linker and debugger and doesn't change a bit in the binary), and the linker didn't complain, I assume that the stack didn't grow into your global variables.

    Even if your parameters list doesn't match the format string, this is no problem. In C, the caller is responsible for removing form stack what it has put on it, so the funciton that calls printf will remove form the stack exactly what has been added before the call.

    You should post your code for further analysis.

  • Jimbo said:
    my call stack is corrupted

    How do you know that it's the Call Stack, specifically, that gets corrupted?

    In what way, exactly, does it get "corrupted" ?

  • Xprintf can be stack-hungry, but the easiest way to damage things with sprintf() is to pass it a string (array) that's too short to hold the result.

    I use snprintf() exclusively -- it's better to truncate my output than to destroy my stack/data/bss.

**Attention** This is a public forum