Using C++ std::vector in task

gwryan wrote on Thursday, October 24, 2013:

Hello. I am new to FreeRTOS and I am experiencing weird behaviour when targeting an ARM STM32L (M3 core-based) board.

I have compiled FreeRTOS with IAR workbench in C (works fine). I added a C++ file, and set it to compile with the C++ compiler (compiles fine).

From main, I have:

extern "C" void do_test(void*);

void main(void)
{
  xTaskCreate( do_test, (signed char*)"do_test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+1, NULL ); 
  vTaskStartScheduler();
  
  for ( ;; );
}

In the C++ file, I have:

#include <vector> 

class my_c {
  unsigned int i;
};

extern "C" void do_test( void* p )
{
  my_c c;
  // std::vector<double> doubles; 
  for (;; );
}

If I leave the call to create the vector<> commented out, everything works fine and I can step through with the debugger without a hitch, even though the program doesn’t really do anything of value.

However, if I uncomment the vector<> declaration, the program will hang before it even gets to main().

Any help would be greatly appreciated.
Thanks,
George

dumarjo wrote on Thursday, October 24, 2013:

HI,


De : George Ryan [mailto:gwryan@users.sf.net]
Envoyé : 24 octobre 2013 10:38
À : [freertos:discussion]
Objet : [freertos:discussion] Using C++ std::vector in task

Hello. I am new to FreeRTOS and I am experiencing weird behaviour when
targeting an ARM STM32L (M3 core-based) board.

I have compiled FreeRTOS with IAR workbench in C (works fine). I added a C++
file, and set it to compile with the C++ compiler (compiles fine).

From main, I have:

extern “C” void do_test(void*);

void main(void)

{

xTaskCreate( do_test, (signed char*)“do_test”, configMINIMAL_STACK_SIZE,
NULL, tskIDLE_PRIORITY+1, NULL );

vTaskStartScheduler();

for ( ;; );

}

In the C++ file, I have:

#include

class my_c {

unsigned int i;

};

extern “C” void do_test( void* p )

{

my_c c;

// std::vector doubles;

for (;; );

}

Have you Overload the new opérator ? If not, you should.

If I leave the call to create the vector<> commented out, everything works
fine and I can step through with the debugger without a hitch, even though
the program doesn’t really do anything of value.

However, if I uncomment the vector<> declaration, the program will hang
before it even gets to main().

Any help would be greatly appreciated.
Thanks,
George

here is a copy of my overloader operator to get it working on my application

// Define the ‘new’ operator for C++ to use the freeRTOS memory management
// functions. THIS IS NOT OPTIONAL!
//
void *operator new(size_t size){
void *p;
#ifdef USE_FREERTOS
if(uxTaskGetNumberOfTasks())
p=pvPortMalloc(size);
else
p=malloc(size);

#else
p=malloc(size);

#endif
#ifdef __EXCEPTIONS
if (p==0) // did pvPortMalloc succeed?
throw std::bad_alloc(); // ANSI/ISO compliant behavior
#endif
return p;
}

//
// Define the ‘delete’ operator for C++ to use the freeRTOS memory
management
// functions. THIS IS NOT OPTIONAL!
//
void operator delete(void *p){
#ifdef USE_FREERTOS
if(uxTaskGetNumberOfTasks())
vPortFree( p );
else
free( p );
#else
free( p );
#endif
p = NULL;
}

void *operator new[](size_t size){
void *p;
#ifdef USE_FREERTOS
if(uxTaskGetNumberOfTasks())
p=pvPortMalloc(size);
else
p=malloc(size);

#else
p=malloc(size);

#endif
#ifdef __EXCEPTIONS
if (p==0) // did pvPortMalloc succeed?
throw std::bad_alloc(); // ANSI/ISO compliant behavior
#endif
return p;
}

//
// Define the ‘delete’ operator for C++ to use the freeRTOS memory
management
// functions. THIS IS NOT OPTIONAL!
//
void operator delete[](void *p){
#ifdef USE_FREERTOS
if(uxTaskGetNumberOfTasks())
vPortFree( p );
else
free( p );
#else
free( p );
#endif
p = NULL;
}

/* Optionally you can override the ‘nothrow’ versions as well.
This is useful if you want to catch failed allocs with your
own debug code, or keep track of heap usage for example,
rather than just eliminate exceptions.
*/

void* operator new(std::size_t size, const std::nothrow_t&) {
return malloc(size);
}

void* operator new[](std::size_t size, const std::nothrow_t&) {
return malloc(size);
}

void operator delete(void* ptr, const std::nothrow_t&) {
free(ptr);
}

void operator delete[](void* ptr, const std::nothrow_t&) {
free(ptr);
}

Regards

Jonathan


Using
<https://sourceforge.net/p/freertos/discussion/382005/thread/93928e86/?limit
=25#a4d0> C++ std::vector in task


Sent from sourceforge.net because you indicated interest in
https://sourceforge.net/p/freertos/discussion/382005/

To unsubscribe from further messages, please visit
https://sourceforge.net/auth/subscriptions/


This message has been scanned for viruses and
dangerous content by http://www.mailscanner.info/ MailScanner, and is
believed to be clean.