Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


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 ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

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

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

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner