# IAR & static\_cast\<\>

**URL:** <https://forums.freertos.org/t/iar-static-cast/537>\
**Category:** Kernel\
**Created:** [June 4, 2008, 10:13am UTC](https://forums.freertos.org/t/iar-static-cast/537 "2008-06-04T10:13:46Z")\
**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:** [June 4, 2008, 10:13am UTC](https://forums.freertos.org/t/iar-static-cast/537/1 "2008-06-04T10:13:46Z")

</div>

**[sam-system](https://sourceforge.net/u/sam-system/)** wrote on [Wednesday, June 04, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/19a2de72/#1db9):

I’m trying to use FreeRTOS together with C++.

I’m using IAR AVR compiler.

All of FreeRTOS source files are compiled using the C compiler and my own code using Extended Embedded C++ compiler. All the code are compiled and linked togther without errors.

Inspired by a richard\_damon post I’ve made a Runable class, see below.

Running my code I get an RStack warning (JTAGICE mkII)  
Wed Jun 04 11:49:06 2008: Target reset  
Wed Jun 04 11:49:16 2008: Program exit reached.  
Wed Jun 04 11:49:17 2008: The stack ‘RStack’ is filled to 100% (32 bytes used out of 32). The warning threshold is set to 90.%

I’ve traced it down to being this static\_cast causing the trouble.

(static\_cast\<Runable\*\>(parm))-\>Run();

If I comment the static\_cast and uncomment the loop causing a led to flash everything works fine.

I have tried the same code using winavr c++ compiler and it works. Using the winavr compiler I compile everything using the c++ compiler. Therefore I have to change some cast here and there in the FreeRTOS code in order to satisfy the c++ compiler. When using IAR I can compile a "clean" FreeRTOS (without changing anything) using C compiler and my own code using C++. My guess is that its the reason for my troubles.

Can someone give me an explanation or even a way to fix it?

/Thomas

// runable.c

#include "runable.h"

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Runable::Runable(unsigned priority, unsigned stacksize)  
{  
#if INCLUDE\_vTaskDelayUntil  
&nbsp; lastWakeTime = NULL;  
#endif  
&nbsp; xTaskCreate(&run, "", stacksize, this, priority, &handle);  
}

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
Runable::~Runable()  
{  
#if INCLUDE\_vTaskDelete  
&nbsp; vTaskDelete(handle);  
#endif&nbsp;  
}

#if INCLUDE\_vTaskSuspend

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
void Runable::Suspend()  
{  
&nbsp; vTaskSuspend(handle);  
}

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
void Runable::Resume()  
{  
&nbsp; vTaskResume(handle);  
}

#endif

#if INCLUDE\_vTaskDelay

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
void Runable::Sleep(portTickType delay)  
{  
&nbsp; vTaskDelay(delay);  
}

#endif

#if INCLUDE\_vTaskDelayUntil

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
void Runable::SleepUntil(portTickType increment)  
{  
&nbsp; if (lastWakeTime == NULL)  
&nbsp;&nbsp;&nbsp; lastWakeTime = xTaskGetTickCount();  
&nbsp; vTaskDelayUntil(&lastWakeTime, increment);  
}

#endif

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
void Runable::run(void\* parm)  
{  
&nbsp; (static\_cast\<Runable\*\>(parm))-\>Run();  
/\*  
&nbsp;&nbsp;&nbsp; for(;😉  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PORTA = (PORTA & ~0x01) | (~PORTA & 0x01);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vTaskDelay( 100 );  
&nbsp;&nbsp;&nbsp; }  
\*/  
}

---

<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:** [June 5, 2008, 7:46pm UTC](https://forums.freertos.org/t/iar-static-cast/537/2 "2008-06-05T19:46:16Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, June 05, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/19a2de72/#1db9/5298):

So the problem seems to be when linking C with C++ code.&nbsp; Is this correct?&nbsp; Can you compile the whole thing as C++ as when using the WinAVR compiler?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 9, 2008, 11:28am UTC](https://forums.freertos.org/t/iar-static-cast/537/3 "2008-06-09T11:28:01Z")

</div>

**[sam-system](https://sourceforge.net/u/sam-system/)** wrote on [Monday, June 09, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/19a2de72/#1db9/3ae6):

I managed to figure it out. I had some errors in my port causing the stack to be currupt.
