# using sprintf on LPC2468, possible stack overflow?

**URL:** <https://forums.freertos.org/t/using-sprintf-on-lpc2468-possible-stack-overflow/1300>\
**Category:** Kernel\
**Created:** [December 17, 2013, 2:52am UTC](https://forums.freertos.org/t/using-sprintf-on-lpc2468-possible-stack-overflow/1300 "2013-12-17T02:52:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 17, 2013, 2:52am UTC](https://forums.freertos.org/t/using-sprintf-on-lpc2468-possible-stack-overflow/1300/1 "2013-12-17T02:52:38Z")

</div>

**[mrtheo](https://sourceforge.net/u/mrtheo/)** wrote on [Tuesday, December 17, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/37c99718/#3d2b):

Hello

I’m want to use sprintf function in my task and I’ve got some problems with it. Here’s the code:

```
void vMyTask(void *pvParameters)
{
while(1)
{
char mytext[50];
sprintf(mytext, "blah blah");
char *pointer = mytext;

xQueueSend(Global_Queue_Handle, &pointer, 1000);
vTaskDelay(5000);
}
}
```

So far, the sprintf function works, data is being sent to httpd-cgi and then to web interface, where it displays “blah blah” text just fine. But if I put some integer in the code, say:

```

char mytext[50];
int value=123;
sprintf(mytext, "%d blah blah", value);
char *pointer = mytext;

```

It doesn’t work - code compiles fine, but the web interface doesn’t even start. I did some searching and found, that this could be a problem with stack overflow, so I’ve increased the stack:

```

xTaskCreate(vMyTask, "MyTask", configMINIMAL_STACK_SIZE, NULL, 1, NULL ); //original
xTaskCreate(vMyTask, "MyTask", 50000, NULL, 1, NULL ); //modified

```

So now the web interface displays fine, but there is no expected “123 blah blah”. And what’s more, if I go back to simple “blah blah” (no integer), it doesn’t display as well - I have to go back to configMINIMAL\_STACK\_SIZE in order for it to display.

And this is where I’ve got so far. What can I do in order to make it work? I’ve seen that there is a low memory cost implementation **printf-stdarg**. Do I use it? How do I use it? I’m fine with any solution that allows me to convert integers (no floats in my code) into chars.

I’m running the code on FreeRTOS v7.5.2 and LPC2468 - 16MB SDRAM seems plenty…

Regards,  
George

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 17, 2013, 9:15am UTC](https://forums.freertos.org/t/using-sprintf-on-lpc2468-possible-stack-overflow/1300/2 "2013-12-17T09:15:31Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, December 17, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/37c99718/#11fb):

> code compiles fine, but the web interface doesn’t even start.

If you view the contents of the mytext buffer in the debugger, does it contain the expected string?

> did some searching and found, that this could be a problem with stack overflow

Enabling stack overflow protection would tell you if that was the case.

> So now the web interface displays fine, but there is no expected “123 blah blah”

You have gone from a tiny stack to a massive stack in one step - is the task even being created? It won’t be unless your heap is in the 16 meg external RAM as you are trying to allocate a 195K stack on a device that has 98K or internal RAM. Checking the return value of xTaskCreate() and defining a malloc failed hook function will help you.

To use printf-stdarg simply include the printf-stdarg C file in your build.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 18, 2013, 1:31pm UTC](https://forums.freertos.org/t/using-sprintf-on-lpc2468-possible-stack-overflow/1300/3 "2013-12-18T13:31:52Z")

</div>

**[mrtheo](https://sourceforge.net/u/mrtheo/)** wrote on [Wednesday, December 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/37c99718/#1a28):

> To use printf-stdarg simply include the printf-stdarg C file in your build.

Well, that was easy. I was expecting having to make some deep changes in the project, so that was why I was asking. Anyway, it works! It’s not perfect, since I have to keep an eye on stack size, as it has a tendency to overflow, but it works!

Using printf-stdarg influenced my rtos stats, which are not displaying since the sprintf function has been modified. But I think I’ll manage with restoring this functionality.

Thank you for your help 🙂
