Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Using C++ std::vector in task

Posted by gwryan on 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( dotest, (signed char*)"dotest", configMINIMALSTACKSIZE, NULL, tskIDLE_PRIORITY+1, NULL ); vTaskStartScheduler();

for ( ;; ); } ~~~~

In the C++ file, I have:

~~~~

include

class my_c { unsigned int i; };

extern "C" void dotest( void* p ) { myc c; // std::vector 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


Using C++ std::vector in task

Posted by dumarjo on 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( dotest, (signed char*)"dotest", configMINIMALSTACKSIZE, 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{ 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{

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::sizet size, const std::nothrowt&) { return malloc(size); }

void* operator new { return malloc(size); }

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

void operator delete { 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.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists